1 tasks:
2 ‐ name: aa
3 模块1
4 when: 条件1
[root@pp ~]# mkdir demo3
[root@pp ~]# cp ansible.cfg hosts demo3/
[root@pp ~]# cd demo3/
[root@pp demo3]#
[root@pp demo3]# cat when-1.yaml
---
- hosts: up
tasks:
- name: tasks1
debug: msg="111"
when: 1<2
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [tasks1] ****************************************************************************
ok: [up] => {
"msg": "111"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]# cat when-1.yaml
---
- hosts: up
tasks:
- name: tasks1
debug: msg="111"
when: 1<2 or 2>3
[root@pp demo3]# ansible-playbook when-1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [tasks1] ****************************************************************************
ok: [up] => {
"msg": "111"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]# cat when-2.yaml
---
- hosts: up
tasks:
- name: tasks2
debug: msg="111"
when: ansible_distribution_major_version == "7"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-2.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [tasks2] ****************************************************************************
skipping: [up]
PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@pp demo3]# cat when-2.yaml
---
- hosts: up
tasks:
- name: tasks2
debug: msg="111"
when: ansible_distribution_major_version == "8"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-2.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [tasks2] ****************************************************************************
ok: [up] => {
"msg": "111"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]#
1 value in 列表
[root@pp demo3]# cat when-3.yaml
---
- hosts: up
vars:
list1: [1,2,3,4]
tasks:
- name: task3
debug: msg="333"
when: 2 in list1
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-3.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [task3] *****************************************************************************
ok: [up] => {
"msg": "333"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]# cat when-3.yaml
---
- hosts: up
vars:
list1: [1,2,3,4]
tasks:
- name: task3
debug: msg="333"
when: 2 not in list1
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-3.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [task3] *****************************************************************************
skipping: [up]
PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@pp demo3]#
[root@pp demo3]# cat /root/demo2/9-inventory1.yaml
---
- hosts: db
tasks:
- name: 打印我在清单文件中的名称
debug: msg={{inventory_hostname}}
when: inventory_hostname in groups ['xx']
[root@pp demo3]#
[root@pp demo3]# cat when-4.yaml
---
- hosts: up
vars:
aa: 1
bb:
tasks:
- name: tasks1
debug: msg="111"
when: aa is undefined
- name: tasks2
debug: msg="222"
when: bb is undefined
- name: tasks3
debug: msg="333"
when: cc is not defined
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-4.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [tasks1] ****************************************************************************
skipping: [up]
TASK [tasks2] ****************************************************************************
skipping: [up]
TASK [tasks3] ****************************************************************************
ok: [up] => {
"msg": "333"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
1 block:
2 ‐ 模块1
3 ‐ 模块2
4 ‐ 模块3
5 rescue:
6 ‐ 模块1
7 ‐ 模块2
[root@pp demo3]# cat block-1.yaml
---
- hosts: up
tasks:
- name: task1
block:
- name: 11
debug: msg="1111"
- name: 22
shell: "ls /aa.txt"
- name: 33
debug: msg="3333"
rescue:
- name: xx
debug: msg="xxxx"
- name: yy
debug: msg="yyyy"
- name: task2
debug: msg="zzzz"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook block-1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [debug] *****************************************************************************
ok: [up] => {
"msg": "1111"
}
TASK [shell] *****************************************************************************
fatal: [up]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.009915", "end": "2023-12-22 11:38:31.526934", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 11:38:31.517019", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
TASK [xx] ********************************************************************************
ok: [up] => {
"msg": "xxxx"
}
TASK [yy] ********************************************************************************
ok: [up] => {
"msg": "yyyy"
}
TASK [task2] *****************************************************************************
ok: [up] => {
"msg": "zzzz"
}
PLAY RECAP *******************************************************************************
up : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
32.3 循环语句
1 for i in A B C ... ; do
2 命令 $
3 done
1 employee:
2 ‐ uname: lisi
3 age: 22
4 sex: man
5
6 ‐ uname: wangwu
7 age: 24
8 sex: man
9
10 ‐ uname: xiaohua
11 age: 21
[root@pp demo3]# cat loop-1.yaml
---
- hosts: up
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 20
sex: woman
tasks:
- name: task1
debug: msg={{ item }}
loop: "{{ users }}"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook loop-1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [task1] *****************************************************************************
ok: [up] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": {
"age": 20,
"sex": "man",
"uname": "tom"
}
}
ok: [up] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": {
"age": 22,
"sex": "man",
"uname": "bob"
}
}
ok: [up] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) => {
"msg": {
"age": 20,
"sex": "woman",
"uname": "mary"
}
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]# cat loop-1.yaml
---
- hosts: up
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 20
sex: woman
tasks:
- name: task1
debug: msg={{ item.uname }}
loop: "{{ users }}"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook loop-1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [task1] *****************************************************************************
ok: [up] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": "tom"
}
ok: [up] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": "bob"
}
ok: [up] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) => {
"msg": "mary"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]# cat loop-1.yaml
---
- hosts: up
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 20
sex: woman
tasks:
- name: task1
debug: msg={{ item.uname }}
when: item.sex == "man"
loop: "{{ users }}"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook loop-1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [task1] *****************************************************************************
ok: [up] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": "tom"
}
ok: [up] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": "bob"
}
skipping: [up] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'})
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo3]#