本章内容主要介绍 playbook 中的控制语句
1 tasks :2 ‐ name : aa3 模块 14 when : 条件 1
[blab@node01 ~]$ mkdir demo3
[blab@node01 ~]$ cp ansible.cfg hosts demo3/
[blab@node01 ~]$ cd demo3
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when1.yml
---
- hosts: node02
tasks:
- name: task1
debug: msg="111"
when: 1 < 2
[blab@node01 demo3]$
????????这里有一个task,判断1<2是否成立,如果成立则执行task1,屏幕上会显示111;如果不成立则不执行taskl,屏幕上不会显示111。这里明显是成立的,所以会执行task1。运行结果如下。
[blab@node01 demo3]$ ansible-playbook when1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
ok: [node02] => {
"msg": "111"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when1.yml
---
- hosts: node02
tasks:
- name: task1
debug: msg="111"
when: 1 < 2 or 2>3
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
ok: [node02] => {
"msg": "111"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when1.yml
---
- hosts: node02
tasks:
- name: task1
debug: msg="111"
when: 1>2 or 2>3
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
skipping: [node02]
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when1.yml
---
- hosts: node02
tasks:
- name: task1
debug: msg="111"
when: 2>1 and 2>3
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
skipping: [node02]
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when1.yml
---
- hosts: node02
tasks:
- name: task1
debug: msg="111"
when: (1>2 or 2!=1) and 2>3
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
skipping: [node02]
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[blab@node01 demo3]$
常见的判断符包括以下6种:
[blab@node01 demo3]$ cat when2.yml
---
- hosts: node02
tasks:
- name: task2
debug: msg="222"
when: ansible_distribution_major_version == "7"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when2.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task2] *******************************************************************
skipping: [node02]
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when2.yml
---
- hosts: node02
tasks:
- name: task2
debug: msg="222"
when: ansible_distribution_major_version == "8"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when2.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task2] *******************************************************************
ok: [node02] => {
"msg": "222"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$
value in 列表
[blab@node01 demo3]$ cat when3.yml
---
- hosts: node02
vars:
list1: [1,2,3,4]
tasks:
- name: task3
debug: msg="333"
when: 2 in list1
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when3.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task3] *******************************************************************
ok: [node02] => {
"msg": "333"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when3.yml
---
- hosts: node02
vars:
list1: [1,2,3,4]
tasks:
- name: task3
debug: msg="333"
when: 2 not in list1 //增加not
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when3.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task3] *******************************************************************
skipping: [node02]
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[blab@node01 demo3]$
‐‐‐
‐ hosts: db
tasks:
‐ name: 打印我在清单文件中的名称
debug: msg={{inventory_hostname}}
when: inventory_hostname in groups ['xx']
is可以用于判断变量是否被定义,常见的判断包括以下3种:
[blab@node01 demo3]$ cat when4.yml
---
- hosts: node02
vars:
aa: 1
bb:
tasks:
- name: task1
debug: msg="111"
when: aa is undefined
- name: task2
debug: msg="222"
when: bb is undefined
- name: task3
debug: msg="333"
when: cc is not defined
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when4.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
skipping: [node02]
TASK [task2] *******************************************************************
skipping: [node02]
TASK [task3] *******************************************************************
ok: [node02] => {
"msg": "333"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat when5.yml
---
- hosts: node02
tasks:
- name: 执行一个系统命令
shell: "ls /aa.txt"
register: aa
ignore_errors: yes
- name: task2
fail: msg="命令执行错了001"
when: aa.rc != 0
- name: task3
debug: msg="OK001"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook when5.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [执行一个系统命令] ****************************************************************
fatal: [node02]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.004728", "end": "2023-12-22 10:18:56.684797", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 10:18:56.680069", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [task2] *******************************************************************
fatal: [node02]: FAILED! => {"changed": false, "msg": "命令执行错了001"}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=1
[blab@node01 demo3]$
1 block :2? ?? ‐ 模块 13 ??? ‐ 模块 24? ?? ‐ 模块 35 rescue :6? ?? ‐ 模块 17? ?? ‐ 模块 2
[blab@node01 demo3]$ cat block1.yml
---
- hosts: node02
tasks:
- name: task1
block:
- name: 11
debug: msg="111"
- name: 22
shell: "ls /aa.txt"
- name: 33
debug: msg="333"
rescue:
- name: xx
debug: msg="xxxx"
- name: yy
debug: msg="yyy"
- name: task2
debug: msg="zzz"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook block1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [debug] *******************************************************************
ok: [node02] => {
"msg": "111"
}
TASK [shell] *******************************************************************
fatal: [node02]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.006491", "end": "2023-12-22 10:51:59.038337", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 10:51:59.031846", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
TASK [xx] **********************************************************************
ok: [node02] => {
"msg": "xxxx"
}
TASK [yy] **********************************************************************
ok: [node02] => {
"msg": "yyy"
}
TASK [task2] *******************************************************************
ok: [node02] => {
"msg": "zzz"
}
PLAY RECAP *********************************************************************
node02 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat block1.yml
---
- hosts: node02
tasks:
- name: task1
block:
- name: 11
debug: msg="111"
- name: 22
shell: "ls /aa.txt"
ignore_errors: yes //增加内容
- name: 33
debug: msg="333"
rescue:
- name: xx
debug: msg="xxxx"
- name: yy
debug: msg="yyy"
- name: task2
debug: msg="zzz"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook block1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [debug] *******************************************************************
ok: [node02] => {
"msg": "111"
}
TASK [shell] *******************************************************************
fatal: [node02]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.003277", "end": "2023-12-22 10:55:41.477248", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 10:55:41.473971", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [debug] *******************************************************************
ok: [node02] => {
"msg": "333"
}
PLAY RECAP *********************************************************************
node02 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[blab@node01 demo3]$
1 for i in A B C ... ; do
2 命令 $
3 done
employee:
‐ uname: lisi
age: 22
sex: man
‐ uname: wangwu
age: 24
sex: man
‐ uname: xiaohua
age: 21
[blab@node01 demo3]$ cat loop1.yml
---
- hosts: node02
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 19
sex: woman
tasks:
- name: task1
debug: msg={{item}}
loop: "{{users}}"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook loop1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
ok: [node02] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": {
"age": 20,
"sex": "man",
"uname": "tom"
}
}
ok: [node02] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": {
"age": 22,
"sex": "man",
"uname": "bob"
}
}
ok: [node02] => (item={'uname': 'mary', 'age': 19, 'sex': 'woman'}) => {
"msg": {
"age": 19,
"sex": "woman",
"uname": "mary"
}
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat loop1.yml
---
- hosts: node02
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 19
sex: woman
tasks:
- name: task1
debug: msg={{item.uname}} //增加内容
loop: "{{users}}"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook loop1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
ok: [node02] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": "tom"
}
ok: [node02] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": "bob"
}
ok: [node02] => (item={'uname': 'mary', 'age': 19, 'sex': 'woman'}) => {
"msg": "mary"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$
[blab@node01 demo3]$ cat loop1.yml
---
- hosts: node02
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 19
sex: woman
tasks:
- name: task1
debug: msg={{item.uname}}
when: item.sex == "man" //增加条件
loop: "{{users}}"
[blab@node01 demo3]$
[blab@node01 demo3]$ ansible-playbook loop1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [task1] *******************************************************************
ok: [node02] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": "tom"
}
ok: [node02] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": "bob"
}
skipping: [node02] => (item={'uname': 'mary', 'age': 19, 'sex': 'woman'})
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo3]$