ansible条件判断

when

[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg="system release is centos"					#“”不能少
    when: ansible_distribution == "CentOS"					#==两端加空格, "CentOS"引号不能少。在when中变量名不用加{{}}

[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg={{item}}
    with_items: [1,2,3]
    when: item > 2

运算比较符:
==
!=
>
<
>=
<=
and
or
not
()

---
- hosts: B
  remote_user: root
  tasks:
  - debug:
      msg: "System release is centos6 or centos7"
    when: ansible_distribution == "CentOS" and (ansible_distribution_major_version == "6" or ansible_distribution_major_version == "7")

ignore_errors

ansible默认机制是当playbook中某个task执行失败,后面的task便不会执行。如想要跳过错误继续执行,需要ignore_errors
[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - shell: ls /data/f1
    register: return						#register:取得shell模块的返回结果并赋值给return
  - debug: msg="success"
    when: teturn.rc == 0				 	#rc:执行状态返回码。0为真,非0为假
  - debug: msg="fail"
    when: return.rc != 0

TASK [shell] *****************************************************************************************************
fatal: [192.168.91.132]: FAILED! => {"changed": true, "cmd": "ls /data/f1", "delta": "0:00:00.004392", "end": "2019-05-31 10:07:10.136161", "msg": "non-zero return code", "rc": 2, "start": "2019-05-31 10:07:10.131769", "stderr": "ls: cannot access /data/f1: No such file or directory", "stderr_lines": ["ls: cannot access /data/f1: No such file or directory"], "stdout": "", "stdout_lines": []}

PLAY RECAP *******************************************************************************************************
192.168.91.132             : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
"non-zero return code", "rc": 2:非零返回码。rc为2.后面的task不会执行

[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - shell: ls /data/f1
    register: return
    ignore_errors: true					#可以用yes代替true
  - debug: msg="success"
    when: teturn.rc == 0
    ignore_errors: true
  - debug: msg="fail"
    when: return.rc != 0

exists

---
- hosts: B
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdir
  tasks:
  - debug:
      msg: "file exist"
    when: testpath is exists				#exists关键字,注意检查的是ansible主机。is not exists表示不存在,也可以用not testpath is exists

判断变量的关键字

defined:判断变量是否已定义。已定义为真
undefined:判断变脸是否已定义。没定义为真
none: 判断变量值是否为空。已定义且为空为真

[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  vars:
   var1: test
   var2:
  tasks:
  - debug: msg="value is defined"
    when: var1 is defined
  - debug: msg="value is undefined"
    when: var3 is undefined
  - debug: msg="value is defined,but have no value"
    when: var2 is none

判断执行结果的关键字

success|succeeded:通过task的返回信息判断任务执行状态,成功为真
failure|failed:通过task的返回信息判断任务执行状态,失败为真
change|changed:通过task的返回信息判断任务执行状态,执行状态为changed为真
skip|skipped:通过task的返回信息判断任务执行状态,当任务被跳过执行时为真

---
- hosts: test70
  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主机,与目标主机无关
file:判断路径是否是一个文件。是文件为真
directory:判断路径是否是一个目录。是目录为真
link:判断路径是否是软连接。是则为真
mount:判断路径是否是挂载点,是则为真
exists:判断路径是否存在,存在为真

---
- hosts: B
  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

判断字符串的关键字

lower:
upper:

[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  vars:
   var1: ABCD
   var2: abcd
  tasks:
  - debug: msg="upper"
    when: var1 is upper
  - debug: msg="lower"
    when: var2 is lower

判断字符串关键字

string:是字符串为真
---
- hosts: B
  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

testvar2,testvar3为真

判断数字关键字

number:对象是数字为真
---
- hosts: B
  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

判断子集父集关键字

subset:
superset:

---
- hosts: B
  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)

判断整除的关键字

even:奇数为真
odd:偶数为真
divisibleby(num):可以整除指定数字为真

---
- hosts: B
  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)

block

在默认情况下,当条件判断成立时,只能执行一个任务。如要执行多个任务。使用block
---
- hosts: B
  remote_user: root
  tasks:
  - debug:
      msg: "task1 not in block"
  - block:
      - debug:
          msg: "task2 in block1"
      - debug:
          msg: "task3 in block1"
    when: 2 > 1

错误处理rescue

错误处理,通常与block一起使用
[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - block:
    - shell: ls /000
    - service: name=httpd state=started 
    rescue:
    - debug: msg="error"

当block中的任意任务执行失败就会执行rescue中的任务

always

在always中的任务总是会被执行
---
- hosts: B
  remote_user: root
  tasks:
  - block:
      - debug:
          msg: 'I execute normally'
      - command: /bin/false						#模拟错误
      - debug:
          msg: 'I never execute, due to the above task failing'
    rescue:
      - debug:
          msg: 'I caught an error'
      - command: /bin/false
      - debug:
          msg: 'I also never execute'
    always:
      - debug:
          msg: "This always executes"

/bin/false执行后,跳到rescue中的debug,然后跳到always

fail模块

自定义一个错误来中断playbook的执行。通常与when一起使用
[root@localhost ~]#vim test.yml 
---
- hosts: B
  remote_user: root
  tasks:
  - debug: msg="1"
  - debug: msg="2"
  - fail: msg="custom error"				#后面的任务不会执行,除非有ignore_errors存在
  - debug: msg="3"

---
- hosts: B
  remote_user: root
  tasks:
  - shell: "echo 'This is a string for testing--error'"
    register: return_value					#把shell执行的结果赋值给register变量,其中包含"error"字符串
  - fail:
      msg: "Conditions established,Interrupt running playbook"
    when: "'error' in return_value.stdout"					#in关键字。当变量中含有“error”字符串为真。注意:“”‘’不能少
  - debug:
      msg: "I never execute,Because the playbook has stopped"

fail_when
---
- hosts: B
  remote_user: root
  tasks:
  - debug:
      msg: "I execute normally"
  - shell: "echo 'This is a string for testing error'"
    register: return_value
    failed_when: ' "error" in return_value.stdout'
  - debug:
      msg: "I never execute,Because the playbook has stopped"
      以上两种格式相同

changed_when

当条件判断为真时,把执行状态从ok改为changed
---
- hosts: B
  remote_user: root
  tasks:
  - debug:
      msg: "test message"
    changed_when: 2 > 1
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhipengit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值