1. exists和not exists
exists:
[root@server4 pd]# cat pd3.yml
---
- hosts: testB
remote_user: root
gather_facts: no
vars:
testpath: /testdir
tasks:
- debug:
msg: "file exist"
when: testpath is exists
测试:
[root@server4 pd]# ansible-playbook pd3.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "file exist"
}
not exists:
格式1:
[root@server4 pd]# cat pd3.yml
---
- hosts: testB
remote_user: root
gather_facts: no
vars:
testpath: /testdirhs
tasks:
- debug:
msg: "file is not exist"
when: testpath is not exists
格式2:
[root@server4 pd]# cat pd3.yml
---
- hosts: testB
remote_user: root
gather_facts: no
vars:
testpath: /testdirhs
tasks:
- debug:
msg: "file is not exist"
when: not testpath is exists
测试:
[root@server4 pd]# ansible-playbook pd3.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "file is not exist"
}
2. defined、undefined、none
[root@server4 pd]# cat pd4.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 is no value"
when: testvar1 is none
测试:
[root@server4 pd]# ansible-playbook pd4.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "variable is defined"
}
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "variable is undefined"
}
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "the variable is defined,but is no value"
}
3. success、failure、change、skip
[root@server4 pd]# cat pd5.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: "failure"
when: returnmsg is failure
- debug:
msg: "change"
when: returnmsg is change
- debug:
msg: "skip"
when: returnmsg is skip
测试:
[root@server4 pd]# ansible-playbook pd5.yml
PLAY [testB] *******************************************************************
TASK [shell] *******************************************************************
fatal: [172.25.60.3]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "cmd": "cat /testdir/abc", "delta": "0:00:00.002901", "end": "2020-03-29 12:45:36.067104", "msg": "non-zero return code", "rc": 1, "start": "2020-03-29 12:45:36.064203", "stderr": "cat: /testdir/abc: No such file or directory", "stderr_lines": ["cat: /testdir/abc: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [debug] *******************************************************************
skipping: [172.25.60.3]
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "failure"
}
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "change"
}
TASK [debug] *******************************************************************
skipping: [172.25.60.3]
版权声明:本文为weixin_43384009原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。