ansible 中的条件判断 和tests

  • 条件判断 和tests
  • 判断变量的一些tests
  • 判断执行结果的一些tests
  • 判断路径的一些tests
  • 判断字符串的一些tests
  • 判断整除的一些tests
  • 判断版本号
  • 判断是否子集或父集
  • 判断是否字符串
  • 判断是否数字


条件判断 和tests

在linux中,我们可以使用test命令进行一些常用的判断操作,比如,使用test命令判断"/testdir"是否存在,示例如下

[root@server4 ~]# test -e /testdir
[root@server4 ~]# echo $?
0
[root@server4 ~]# 
[root@server4 ~]# test -e /optt
[root@server4 ~]# echo $?
1

上述命令表示判断"/testdir"是否存在于系统中,如果"/testdir"存在,则返回true,如果"/testdir"不存在,则返回false,而在linux中,命令的返回值为0表示true,返回值为非0表示false,上例的返回值为0,所以"/testdir"存在于文件系统中,我们也可以在shell脚本中使用test命令进行判断,示例如下

[root@server4 ~]# vim test.sh
[root@server4 ~]# cat test.sh
#!/bin/bash
if test -e /testdir; then
  echo "testdir exist"
fi
[root@server4 ~]# sh test.sh 
/testdir exist

其实,在ansible中,也有类似的用法,只不过ansible没有使用linux的test命令,而是使用了jinja2的tests,借助tests,我们可以进行一些判断操作,tests会将判断后的布尔值返回,如果条件成立,则返回true,如果条件不成立,tests会返回false,我们通常会在条件判断时使用到tests,那么怎样在ansible中使用jinja2的tests进行判断呢?我们先来看一个小例子,示例如下:

[root@server4 ~]# vim pd5.yml
[root@server4 ~]# cat pd5.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdir
  tasks:
  - debug:
      msg: "file exist"
    when: testpath is exists

ansible 检查目录是否存在 ansible检查文件是否存在_服务器


如上例所示,我们定义了一个testpath变量,这个变量的值是"/testdir"路径,我通过when判断"/testdir"路径是否存在,"is exists"中的"exists"就是tests的一种,它与"test -e"命令的作用是相同的,通过"exists"可以判断ansible主机中的对应路径是否存在(注意:是ansible控制主机中的路径,与目标主机没有关系),当对应的路径存在于ansible控制节点时,"is exists"为真。

“is exists"可以在路径存在时返回真,但是有时,我们想要在路径不存在时返回真,我们可以使用"is not exists”,"is not exists"表示对应路径不存在时返回真,示例如下:

[root@server4 ~]# vim pd5.yml
[root@server4 ~]# cat pd5.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdirrr
  tasks:
  - debug:
      msg: "file not exist"
    when: testpath is not exists

ansible 检查目录是否存在 ansible检查文件是否存在_运维_02


我们可以看出,在ansible中,"is exists"表示如果路径存在于ansible节点则返回真,"is not exists"表示如果路径不存在于ansible节点则返回真,当我们使用一种tests进行条件判断时,在tests前面加上"is"进行判断,也可以在tests前面加上"is not"进行取反的判断,当然,在前一篇文章中,我们已经总结了取反的逻辑操作符,所以,我们也可以对整个条件取反,比如如下示例

[root@server4 ~]# vim pd5.yml
[root@server4 ~]# cat pd5.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdirrr
  tasks:
  - debug:
      msg: "file not exist"
    when: not testpath is exists

ansible 检查目录是否存在 ansible检查文件是否存在_linux_03

上例取反的效果与"is not"的效果相同。

在ansible中,除了能够使用"exists"这种tests,还有一些别的tests能够使用,我们来认识一下这些tests

判断变量的一些tests

  • defined :判断变量是否已经定义,已经定义则返回真
  • undefind :判断变量是否已经定义,未定义则返回真
  • none :判断变量值是否为空,如果变量已经定义,但是变量值为空,则返回真

上述tests的使用示例如下:

[root@server4 ~]# vim pd6.yml
[root@server4 ~]# cat pd6.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar: "test"
    testvar1:
  tasks:
  - debug:
      msg: "Variable is defined"
    when: testvar is defined
  - debug:
      msg: "Variable is undefined"
    when: testvar2 is undefined
  - debug:
      msg: "The variable is defined, but there is no value"
    when: testvar1 is none

当对应的条件为真时,你可以看到debug模块对应的输出:

ansible 检查目录是否存在 ansible检查文件是否存在_运维_04

判断执行结果的一些tests

  • success 或 succeeded:通过任务的返回信息判断任务的执行状态,任务执行成功则返回真
  • failure 或 failed:通过任务的返回信息判断任务的执行状态,任务执行失败则返回真
  • change 或 changed:通过任务的返回信息判断任务的执行状态,任务执行状态为changed则返回真
  • skip 或 skipped:通过任务的返回信息判断任务的执行状态,当任务没有满足条件,而被跳过执行时,则返回真

上述tests的使用示例如下:

[root@server4 ~]# vim pd7.yml
[root@server4 ~]# cat pd7.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    doshell: "yes"
  tasks:
  - shell: "cat /testdir/abc"
    when: doshell == "yes"
    register: returnmsg
    ignore_errors: true
  - debug:
      msg: "success"
    when: returnmsg is success
  - debug:
      msg: "failed"
    when: returnmsg is failure
  - debug:
      msg: "changed"
    when: returnmsg is change
  - debug:
      msg: "skip"
    when: returnmsg is skip

ansible 检查目录是否存在 ansible检查文件是否存在_ansible 检查目录是否存在_05


ansible 检查目录是否存在 ansible检查文件是否存在_centos_06

如上例所示,我们调用了shell模块,将shell模块的返回信息注册在了returnmsg变量中,之后的debug任务均通过returnmsg变量判断shell模块的执行状态,因为shell模块有可能执行失败,所以,我们为shell模块添加了"ignore_errors: true",以便即使shell模块执行失败,也能执行后面的任务,并且,我为shell模块添加了判断条件,当不满足条件时,shell模块则会跳过,即不会执行,你可以修改一下条件,以便测试skip的判断效果。

判断路径的一些tests

注:如下tests的判断均针对于ansible主机中的路径,与目标主机无关

  • file : 判断路径是否是一个文件,如果路径是一个文件则返回真
  • directory :判断路径是否是一个目录,如果路径是一个目录则返回真
  • link :判断路径是否是一个软链接,如果路径是一个软链接则返回真
  • mount:判断路径是否是一个挂载点,如果路径是一个挂载点则返回真
  • exists:判断路径是否存在,如果路径存在则返回真

注:上述test名均为2.6版本中的名称,在2.5版本之前某些test需要加上"is_"前缀

上述tests的使用示例如下:

[root@server4 ~]# vim pd8.yml
[root@server4 ~]# cat pd8.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath1: "/testdir/test"
    testpath2: "/testdir/"
    testpath3: "/testdir/testsoftlink"
    testpath4: "/testdir/testhardlink"
    testpath5: "/boot"
  tasks:
  - debug:
      msg: "file"
    when: testpath1 is file
  - debug:
      msg: "directory"
    when: testpath2 is directory
  - debug:
      msg: "link"
    when: testpath3 is link
  - debug:
      msg: "link"
    when: testpath4 is link
  - debug:
      msg: "mount"
    when: testpath5 is mount
  - debug:
      msg: "exists"
    when: testpath1 is exists

建立测试文件:

[root@server4 ~]# cd /testdir/
[root@server4 testdir]# touch test
[root@server4 testdir]# ln -s test testsoftlink
[root@server4 testdir]# ln test testhardlink

ansible 检查目录是否存在 ansible检查文件是否存在_centos_07


playbook运行结果:

ansible 检查目录是否存在 ansible检查文件是否存在_服务器_08

ansible 检查目录是否存在 ansible检查文件是否存在_linux_09


由于testhardlink为硬链接,而is link判断只有为软链接时才会返回真,因此这个task被跳过。

判断字符串的一些tests

  • lower:判断包含字母的字符串中的字母是否是纯小写,字符串中的字母全部为小写则返回真
  • upper:判断包含字母的字符串中的字母是否是纯大写,字符串中的字母全部为大写则返回真

上述tests的使用示例如下:

[root@server4 ~]# vim pd9.yml
[root@server4 ~]# cat pd9.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    str1: "abc"
    str2: "ABC"
  tasks:
  - debug:
      msg: "This string is all lowercase"
    when: str1 is lower
  - debug:
      msg: "This string is all uppercase"
    when: str2 is upper

ansible 检查目录是否存在 ansible检查文件是否存在_linux_10

判断整除的一些tests

  • even :判断数值是否是偶数,是偶数则返回真
  • odd :判断数值是否是奇数,是奇数则返回真
  • divisibleby(num) :判断是否可以整除指定的数值(num),如果除以指定的值以后余数为0,则返回真

上述tests的使用示例如下:

[root@server4 ~]# vim pd10.yml
[root@server4 ~]# cat pd10.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    num1: 4
    num2: 7
    num3: 64
  tasks:
  - debug:
      msg: "An even number"
    when: num1 is even
  - debug:
      msg: "An odd number"
    when: num2 is odd
  - debug:
      msg: "Can be divided exactly by"
    when: num3 is divisibleby(8)

ansible 检查目录是否存在 ansible检查文件是否存在_服务器_11

判断版本号

  • version:可以用于对比两个版本号的大小,或者与指定的版本号进行对比,使用语法为 version(‘版本号’, ‘比较操作符’)

注:2.5版本中此tests从version_compare更名为version

示例如下

[root@server4 ~]# vim pd11.yml
[root@server4 ~]# cat pd11.yml 
---
- hosts: testB
  remote_user: root
  vars:
    ver: 7.4.1708
    ver1: 7.4.1707
  tasks:
  - debug:
      msg: "This message can be displayed when the ver is greater than ver1"
    when: ver is version(ver1,">")
  - debug:
      msg: "system version {{ansible_distribution_version}} greater than 7.2"
    when: ansible_distribution_version is version("7.2","gt")

ansible 检查目录是否存在 ansible检查文件是否存在_ansible 检查目录是否存在_12


注:我们使用的系统版本为7.3

上例中有两个task

第一个task中,当ver的版本号大于ver1时,返回真,条件成立,debug模块输出"This message can be displayed when the ver is greater than ver1"

第二个task中,当facts中的ansible_distribution_version的值大于7.2时,返回真,条件成立,debug模块输出对应信息

我们可以发现,">“与"gt"都表示"大于”,当使用version时,支持多种风格的比较操作符,你可以根据自己的使用习惯进行选择,version支持的比较操作符如下

大于:>, gt

大于等于:>=, ge

小于:<, lt

小于等于:<=, le

等于: ==, =, eq

不等于:!=, <>, ne

判断是否子集或父集

  • subset:判断一个list是不是另一个list的子集,是另一个list的子集时返回真
  • superset : 判断一个list是不是另一个list的父集,是另一个list的父集时返回真

注:2.5版本中上述两个tests从issubset和issuperset更名为subset和superset

示例如下

[root@server4 ~]# vim pd12.yml
[root@server4 ~]# cat pd12.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    a:
    - 2
    - 5
    b: [1,2,3,4,5]
  tasks:
  - debug:
      msg: "A is a subset of B"
    when: a is subset(b)
  - debug:
      msg: "B is the parent set of A"
    when: b is superset(a)

ansible 检查目录是否存在 ansible检查文件是否存在_linux_13

如上图所示,很明显我们可以看出[2,5]是[1,2,3,4,5]的子集。

判断是否字符串

  • string:判断对象是否是一个字符串,是字符串则返回真
[root@server4 ~]# vim pd13.yml
[root@server4 ~]# cat pd13.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar1: 1
    testvar2: "1"
    testvar3: a
  tasks:
  - debug:
      msg: "This variable is a string"
    when: testvar1 is string
  - debug:
      msg: "This variable is a string"
    when: testvar2 is string
  - debug:
      msg: "This variable is a string"
    when: testvar3 is string

ansible 检查目录是否存在 ansible检查文件是否存在_linux_14

上例playbook中只有testvar2(“1”)和testvar3(a)会被判断成字符串,testvar1(1)不会

判断是否数字

  • number:判断对象是否是一个数字,是数字则返回真

示例如下

[root@server4 ~]# vim pd14.yml
[root@server4 ~]# cat pd14.yml 
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar1: 1
    testvar2: "1"
    testvar3: 00.20
  tasks:
  - debug:
      msg: "This variable is number"
    when: testvar1 is number
  - debug:
      msg: "This variable is a number"
    when: testvar2 is number
  - debug:
      msg: "This variable is a number"
    when: testvar3 is number

ansible 检查目录是否存在 ansible检查文件是否存在_linux_15

上例playbook中只有testvar1(1)和testvar3(00.20)会被判断成数字,testvar2(“1”)不会。