1. when
[root@server4 pd]# cat pd.yml # 当item大于1时输出
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_items:
- 1
- 2
- 3
when: item > 1
测试:
[root@server4 pd]# ansible-playbook pd.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
skipping: [172.25.60.3] => (item=1)
ok: [172.25.60.3] => (item=2) => {
"msg": 2
}
ok: [172.25.60.3] => (item=3) => {
"msg": 3
}
2. 获取ansible主机的信息
[root@server4 pd]# ansible testB -m setup| grep ansible_distribution
"ansible_distribution": "RedHat",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_search_string": "Red Hat",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Maipo",
"ansible_distribution_version": "7.5",
3. 判断主机信息是否一致
写法1:
[root@server4 pd]# ls
pd1.yml pd.yml
[root@server4 pd]# cat pd1.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "system release is redhat7"
when: ansible_distribution == "RedHat" and ansible_distribution_major_version == "7"
写法2:
- [ ] [root@server4 pd]# cat pd1.yml
---
- hosts: testB remote_user: root tasks:
- debug:
msg: "system release is redhat7"
when:
- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "7"
测试:
[root@server4 pd]# ansible-playbook pd1.yml
PLAY [testB] *************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [172.25.60.3]
TASK [debug] *************************************************************************************************************************************
ok: [172.25.60.3] => {
"msg": "system release is redhat7"
}
4. not的使用
[root@server4 pd]# cat pd1.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "system release is not windows"
when: not ansible_distribution == "Windows"
测试:
[root@server4 pd]# ansible-playbook pd1.yml
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.60.3]
TASK [debug] *******************************************************************
ok: [172.25.60.3] => {
"msg": "system release is not windows"
}
5. ignore_errors和.rc判断
[root@server4 pd]# cat pd2.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
shell: "ls /testhaha"
register: returnmsg
ignore_errors: true # 如果return的返回结果有误,则忽略
- name: task2
debug:
msg: "successful"
when: returnmsg.rc == 0 # rc==0表示shell的内容执行成功
- name: task3
debug:
msg: "failed"
when: returnmsg.rc != 0 # rc==0表示shell的内容执行不成功
测试:
[root@server4 pd]# ansible-playbook pd2.yml
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.60.3]
TASK [task1] *******************************************************************
fatal: [172.25.60.3]: FAILED! => {"changed": true, "cmd": "ls /testhaha", "delta": "0:00:00.003328", "end": "2020-03-29 11:35:02.442537", "msg": "non-zero return code", "rc": 2, "start": "2020-03-29 11:35:02.439209", "stderr": "ls: cannot access /testhaha: No such file or directory", "stderr_lines": ["ls: cannot access /testhaha: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [task2] *******************************************************************
skipping: [172.25.60.3]
TASK [task3] *******************************************************************
ok: [172.25.60.3] => {
"msg": "failed"
}