如下例所示,使用when关键字指明条件,条件是ansible_distribution的值是CentOS,细心如你一定已经发现了,ansible_distribution就是facts信息中的一个key,通过ansible_distribution可以获取到目标主机系统的发行版,在之前的文章中,如果我们需要获取到facts中的key的值,都是通过引用变量的方式获取的,即”{{ key }}”,但是,在使用when关键字时,我们并没有为ansible_distribution添加”{{ }}”,没错,在when关键字中引用变量时,变量名不需要加”{{ }}”, 那么上例playbook表示,如果ansible_distribution的值是CentOS,则调用debug模块,输出”System release is centos”这句话,如果ansible_distribution的值不是CentOS,则不满足条件,则不会调用debug模块
[root@localhost ~]# ansible -i test_hosts test70 -m setup -a "filter=*ansible_distribution*"
test70 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.7",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[root@localhost ~]# cat 6.yml
---
- hosts: test70
remote_user: root
tasks:
- debug:
msg: "System release is centos"
when: ansible_distribution == "CentOS"
[root@localhost ~]# ansible-playbook -i test_hosts 6.yml
PLAY [test70] ***************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [test70]
TASK [debug] ****************************************************************************************************************************************************************************************************
ok: [test70] => {
"msg": "System release is centos"
}
PLAY RECAP ******************************************************************************************************************************************************************************************************
test70 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
我们再来看一个用于循环的条件判断示例,当item的值大于1时,才会调用debug模块输出对应的信息,如下:
[root@localhost ~]# cat 7.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_items:
- 1
- 2
- 3
when: item > 1
[root@localhost ~]# ansible-playbook -i test_hosts 7.yml
PLAY [test70] ***************************************************************************************************************************************************************************************************
TASK [debug] ****************************************************************************************************************************************************************************************************
skipping: [test70] => (item=1)
ok: [test70] => (item=2) => {
"msg": 2
}
ok: [test70] => (item=3) => {
"msg": 3
}
PLAY RECAP ******************************************************************************************************************************************************************************************************
test70 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
比较运算符
== :比较两个对象是否相等,相等为真
!= :比较两个对象是否不等,不等为真
> :比较两个值的大小,如果左边的值大于右边的值,则为真
< :比较两个值的大小,如果左边的值小于右边的值,则为真
>= :比较两个值的大小,如果左边的值大于右边的值或左右相等,则为真
<= :比较两个值的大小,如果左边的值小于右边的值或左右相等,则为真
逻辑运算符
and :逻辑与,当左边与右边同时为真,则返回真
or :逻辑或,当左边与右边有任意一个为真,则返回真
not :取反,对一个操作体取反
( ) :组合,将一组操作体包装在一起,形成一个较大的操作体
当我们调用shell模块运行命令时,通常需要获取到shell模块的返回信息,以便之后的模块能够根据返回信息的值判断之后进行怎样的操作,示例如下:
我们想要通过shell模块在远程主机test70中执行命令 “ls /testabc”,我们将shell模块的返回值注册到了变量returnmsg,然后通过returnmsg获取到了命令执行的返回码,如果返回码为0,则证明命令完全正常执行,如果返回码不为0,则证明命令执行时出错了。
[root@localhost ~]# cat 8.yml
---
- hosts: test70
remote_user: root
tasks:
- name: task1
shell: "ls /testabc"
register: returnmsg
ignore_errors: true
- name: task2
debug:
msg: "Command execution successful"
when: returnmsg.rc == 0
- name: task3
debug:
msg: "Command execution failed"
when: returnmsg.rc != 0
[root@localhost ~]# ansible-playbook -i test_hosts 8.yml
PLAY [test70] ***************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [test70]
TASK [task1] ****************************************************************************************************************************************************************************************************
fatal: [test70]: FAILED! => {"changed": true, "cmd": "ls /testabc", "delta": "0:00:00.003118", "end": "2023-05-05 17:23:59.202775", "msg": "non-zero return code", "rc": 2, "start": "2023-05-05 17:23:59.199657", "stderr": "ls: cannot access /testabc: No such file or directory", "stderr_lines": ["ls: cannot access /testabc: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [task2] ****************************************************************************************************************************************************************************************************
skipping: [test70]
TASK [task3] ****************************************************************************************************************************************************************************************************
ok: [test70] => {
"msg": "Command execution failed"
}
PLAY RECAP ******************************************************************************************************************************************************************************************************
test70 : ok=3 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=1
ansible默认的机制是当playbook中的某个task执行后,如果task报错,playbook会在报错task的位置上停止,报错task之后的task则不会执行。通过”ignore_errors”关键字即使当前task执行报错,ansible也会忽略这个错误,继续执行playbook。