目录
1.判断语句When
1.1 When判断中>、<、!=的使用
练习1:写一个playbook,判断某条件是否成立,成立了才执行task,否则不执行,命令如下
练习2:修改when-1.yaml的内容如下。
练习3:修改when-1.yaml的内容如下。
练习4:修改when-1.yaml的内容如下。
练习5:修改 when-1.yaml 的内容如下。
常见的判断符包括以下6种。
练习6:如果server2的系统主版本是7(RHEL/CentOS7),则打印111,否则不打印。playbook的内容如下
练习7:修改when-2.yaml 的内容如下。
1.2 When判断in的用法
练习:判断某值是否在列表中,编写 when-3.yaml,命令如下
1.3 When判断is的用法
2. 判断语句block-rescue
练习1:按上面的描述写一个playbook,内容如下。
练习2: 修改block-1.yaml的内容如下。(添加ignore_erros: yes)
3. 循环语句
练习1:定义一个列表users,然后循环这个列表中的每个元素,命令如下。
练习2:修改loop-1.yaml的内容如下。
练习3:如果想打印所有性别为男的那些用户名,修改loop-1.yaml 的内容如下。
tasks:
‐ name: aa
模块1
when: 条件1
注意在when中引用变量时是不用加{{}}的。
[bdqn@RHEL813 ~]$ mkdir demo3
[bdqn@RHEL813 ~]$ cp ansible.cfg hosts demo3/
[bdqn@RHEL813 ~]$ cd demo3
[bdqn@RHEL813 demo3]$ ls
ansible.cfg hosts
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
debug: msg="111"
when: 1<2
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
ok: [server2] => {
"msg": "111"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
debug: msg="111"
when: 1 < 2 or 2 > 3
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
ok: [server2] => {
"msg": "111"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
debug: msg="111"
when: 1 > 2 or 2 > 3
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
skipping: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
debug: msg="111"
when: 2 > 1 and 2 > 3
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
skipping: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
debug: msg="111"
when: (1 > 2 or 2 != 1) and 2 > 3
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
skipping: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-2.yaml
---
- hosts: server2
tasks:
- name: task2
debug: msg="111"
when: ansible_distribution_major_version == "7"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [task2] ************************************************************************************************************************
skipping: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
注意ansible_distribution major version的值是一个字符串,所以when判断中=后面的7是要加引号的
[bdqn@RHEL813 demo3]$ cat when-2.yaml
---
- hosts: server2
tasks:
- name: task2
debug: msg="111"
when: ansible_distribution_major_version == "8"
[bdqn@RHEL813 demo3]$
再次运行此playbook,命令如下,会显示111。
[bdqn@RHEL813 demo3]$ ansible-playbook when-2.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [task2] ************************************************************************************************************************
ok: [server2] => {
"msg": "111"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
再次提醒:在when 中引用变量时是不用加{{}}的
value in 列表
[bdqn@RHEL813 demo3]$ cat when-3.yaml
---
- hosts: server2
vars:
list1: [1,2,3,4]
tasks:
- name: tasks3
debug: msg="333"
when: 2 in list1
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-3.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks3] ***********************************************************************************************************************
ok: [server2] => {
"msg": "333"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-3.yaml
---
- hosts: server2
vars:
list1: [1,2,3,4]
tasks:
- name: tasks3
debug: msg="333"
when: 2 not in list1
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-3.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks3] ***********************************************************************************************************************
skipping: [server2]
PLAY RECAP **************************************************************************************************************************
server2 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 ~]$ cat demo2/9-inventory1.yaml
---
- hosts: db
tasks:
- name: 打印 我在清单中的名称
debug: msg={{inventory_hostname}}
when: inventory_hostname in groups ['xx']
[bdqn@RHEL813 ~]$
(1)is defined:变量被定义。
(2)is undefined:等同于is not defined,变量没有被定义。
(3)is none:变量被定义了,但是值为空。
[bdqn@RHEL813 demo3]$ cat when-4.yaml
---
- hosts: server2
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
[bdqn@RHEL813 demo3]$
(1)如果aa被定义了,则显示111,这里aa被定义了,所以判断成立,会执行task1。
(2)如果b没有被定义,则显示222,这里bb被定义了,所以判断不成立,不会执行
task2。
(3)如果cc没有被定义,则显示333,这里cc没有被定义,所以判断成立,会执行
task3。
[bdqn@RHEL813 demo3]$ ansible-playbook when-4.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
skipping: [server2]
TASK [tasks2] ***********************************************************************************************************************
skipping: [server2]
TASK [tasks3] ***********************************************************************************************************************
ok: [server2] => {
"msg": "333"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat when-5.yaml
---
- hosts: server2
tasks:
- name: 执行一个系统命令
shell: "ls /aa.txt"
register: aa
ignore_errors: yes
- name: tasks2
fail: msg="命令执行错了001"
when: aa.rc != 0
- name: tasks3
debug: msg="OK123"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook when-5.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [执行一个系统命令] *********************************************************************************************************************
fatal: [server2]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.008831", "end": "2023-12-22 10:18:14.551873", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 10:18:14.543042", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [tasks2] ***********************************************************************************************************************
fatal: [server2]: FAILED! => {"changed": false, "msg": "命令执行错了001"}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=1
[bdqn@RHEL813 demo3]$
block-rescue的用法如下。
block:
‐ 模块1
‐ 模块2
‐ 模块3
rescue:
‐ 模块1
‐ 模块2
如果rescue中的某个模块执行失败,则退出整个playbook,如图32-3所示。
[bdqn@RHEL813 demo3]$ cat block-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
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: tasks2
debug: msg="zzzz"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook block-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [debug] ************************************************************************************************************************
ok: [server2] => {
"msg": "1111"
}
TASK [shell] ************************************************************************************************************************
fatal: [server2]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.005576", "end": "2023-12-22 10:46:34.520144", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 10:46:34.514568", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
TASK [xx] ***************************************************************************************************************************
ok: [server2] => {
"msg": "xxxx"
}
TASK [yy] ***************************************************************************************************************************
ok: [server2] => {
"msg": "yyyy"
}
TASK [tasks2] ***********************************************************************************************************************
ok: [server2] => {
"msg": "zzzz"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat block-1.yaml
---
- hosts: server2
tasks:
- name: tasks1
block:
- name: 11
debug: msg="1111"
- name: 22
shell: "ls /aa.txt"
ignore_errors: yes
- name: 33
debug: msg="3333"
rescue:
- name: xx
debug: msg="xxxx"
- name: yy
debug: msg="yyyy"
- name: tasks2
debug: msg="zzzz"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook block-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [debug] ************************************************************************************************************************
ok: [server2] => {
"msg": "1111"
}
TASK [shell] ************************************************************************************************************************
fatal: [server2]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.005930", "end": "2023-12-22 10:50:16.769375", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 10:50:16.763445", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [debug] ************************************************************************************************************************
ok: [server2] => {
"msg": "3333"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[bdqn@RHEL813 demo3]$
for i in A B C ... ; do
命令 $
done
再回顾一下前面介绍的列表,如下所示。
employee:
‐ uname: lisi
age: 22
sex: man
‐ uname: wangwu
age: 24
sex: man
‐ uname: xiaohua
age: 21
[bdqn@RHEL813 demo3]$ cat loop-1.yaml
---
- hosts: server2
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 20
sex: woman
tasks:
- name: tasks1
debug: msg={{ item }}
loop: "{{ users }}"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook loop-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
ok: [server2] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": {
"age": 20,
"sex": "man",
"uname": "tom"
}
}
ok: [server2] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": {
"age": 22,
"sex": "man",
"uname": "bob"
}
}
ok: [server2] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) => {
"msg": {
"age": 20,
"sex": "woman",
"uname": "mary"
}
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat loop-1.yaml
---
- hosts: server2
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 20
sex: woman
tasks:
- name: tasks1
debug: msg={{ item.uname }}
loop: "{{ users }}"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook loop-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
ok: [server2] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": "tom"
}
ok: [server2] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": "bob"
}
ok: [server2] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) => {
"msg": "mary"
}
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ cat loop-1.yaml
---
- hosts: server2
vars:
users:
- uname: tom
age: 20
sex: man
- uname: bob
age: 22
sex: man
- uname: mary
age: 20
sex: woman
tasks:
- name: tasks1
debug: msg={{ item.uname }}
when: item.sex == "man"
loop: "{{ users }}"
[bdqn@RHEL813 demo3]$
[bdqn@RHEL813 demo3]$ ansible-playbook loop-1.yaml
PLAY [server2] **********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [server2]
TASK [tasks1] ***********************************************************************************************************************
ok: [server2] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {
"msg": "tom"
}
ok: [server2] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {
"msg": "bob"
}
skipping: [server2] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'})
PLAY RECAP **************************************************************************************************************************
server2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[bdqn@RHEL813 demo3]$