做软装素材从哪些网站找,云主机如何建网站,关于设计图的网站,做医疗的网站建设本章主要介绍 playbook中的控制语句。 使用 when 判断语句 block-rescue判断 循环语句 一个play中可以包含多个task#xff0c;如果不想所有的task全部执行#xff0c;可以设置只有满足某个 条件才执行这个task#xff0c;不满足条件则不执行此task。本章主要讲解when 和 … 本章主要介绍 playbook中的控制语句。 使用 when 判断语句 block-rescue判断 循环语句 一个play中可以包含多个task如果不想所有的task全部执行可以设置只有满足某个 条件才执行这个task不满足条件则不执行此task。本章主要讲解when 和 block-rescue两 种判断语句。 32.1 判断语句when when作为一个判断语句出现在某个 task下格式如下。 1 tasks:
2 ‐ name: aa
3 模块1
4 when: 条件1 如果条件1成立则执行模块1否则不执行。 注意 在when中引用变量时是不用加{{}}的。 本章实验都在/home/duan/demo3下操作先把 demo3目录创建出来并把ansible.cfg 和 hosts拷贝进去命令如下。 [rootpp ~]# mkdir demo3
[rootpp ~]# cp ansible.cfg hosts demo3/
[rootpp ~]# cd demo3/
[rootpp demo3]# 32.1.1 when判断中、、!的使用 练习1写一个playbook判断某条件是否成立成立了才执行task否则不执行命令 如下。 [rootpp demo3]# cat when-1.yaml
---
- hosts: uptasks:- name: tasks1debug: msg111when: 12
[rootpp demo3]# 这里有一个task判断12是否成立如果成立则执行task1屏幕上会显示111;如果不 成立则不执行taskl屏幕上不会显示111。这里明显是成立的所以会执行task1。运行结 果如下。 [rootpp demo3]# ansible-playbook when-1.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks1] ****************************************************************************
ok: [up] {msg: 111
}PLAY RECAP *******************************************************************************
up : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 hen后面可以有多个条件用or或and作为连接符。 如果用or作为连接符,只要有一个条件成立即可只有所有的条件都不成立时,整体才不成 立。 练习2修改when-1.yaml的内容如下。 [rootpp demo3]# cat when-1.yaml
---
- hosts: uptasks:- name: tasks1debug: msg111when: 12 or 23此处用or作为连接符只要有一个条件成立就会成立23不成立但是12成立所以 整体上就是成立的。运行结果如下。 [rootpp demo3]# ansible-playbook when-1.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks1] ****************************************************************************
ok: [up] {msg: 111
}PLAY RECAP *******************************************************************************
up : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 常见的判断符包括以下6种。 1等于。 2!不等于。 3大于。 4大于等于。 5小于。 6小于等于。 练习如果server2的系统主版本是7(RHEL/CentOS7)则打印111否则不打印。 playbook的内容如下。 [rootpp demo3]# cat when-2.yaml
---
- hosts: uptasks:- name: tasks2debug: msg111when: ansible_distribution_major_version 7
[rootpp demo3]# 因为server2的系统是RHEL8所以不会执行此task2即不会显示111。 [rootpp demo3]# ansible-playbook when-2.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks2] ****************************************************************************
skipping: [up]PLAY RECAP *******************************************************************************
up : ok1 changed0 unreachable0 failed0 skipped1 rescued0 ignored0 注意 ansible_distribution major version的值是一个字符串所以when判断中后面的7是要加引号 的。 练习2修改when-2.yaml 的内容如下。 [rootpp demo3]# cat when-2.yaml
---
- hosts: uptasks:- name: tasks2debug: msg111when: ansible_distribution_major_version 8
[rootpp demo3]# 再次运行此playbook,命令如下,会显示111。 [rootpp demo3]# ansible-playbook when-2.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks2] ****************************************************************************
ok: [up] {msg: 111
}PLAY RECAP *******************************************************************************
up : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [rootpp demo3]# 再次提醒在when 中引用变量时是不用加{{}}的。 32.1.2 when判断中in的用法 在when语句中除可以使用上面的大于、小于等判断方法外还可以使用 in用法如 下。 1 value in 列表 如果此值在这个列表中,则判断成立否则不成立。 练习判断某值是否在列表中编写 when-3.yaml命令如下。 [rootpp demo3]# cat when-3.yaml
---
- hosts: upvars:list1: [1,2,3,4]tasks:- name: task3debug: msg333when: 2 in list1
[rootpp demo3]# 此处定义了一个列表 list1里面有4个值分别为1、2、3、4;定义了一个task打印333 会不会执行这个task就要看when后面的判断是否成立。如果2在列表list1中则执行;如果 不在则不执行很明显2在列表list1中所以会执行此task即屏幕上会显示333。运行结 果如下。 [rootpp demo3]# ansible-playbook when-3.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task3] *****************************************************************************
ok: [up] {msg: 333
}PLAY RECAP *******************************************************************************
up : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 因为2在列表list1中when判断成立可以正确执行task3所以屏幕上会显示333。修 改when-3.yaml的内容如下。 [rootpp demo3]# cat when-3.yaml
---
- hosts: upvars:list1: [1,2,3,4]tasks:- name: task3debug: msg333when: 2 not in list1
[rootpp demo3]# 这里判断的是2不在列表list1中但2是在列表list1中的所以判断不成立。运行结果如 下。 [rootpp demo3]# ansible-playbook when-3.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task3] *****************************************************************************
skipping: [up]PLAY RECAP *******************************************************************************
up : ok1 changed0 unreachable0 failed0 skipped1 rescued0 ignored0 [rootpp demo3]# 因为when判断不成立所以屏幕上不会显示333。回想前面的例子。 [rootpp demo3]# cat /root/demo2/9-inventory1.yaml
---
- hosts: dbtasks:- name: 打印我在清单文件中的名称debug: msg{{inventory_hostname}}when: inventory_hostname in groups [xx]
[rootpp demo3]# 这里判断当前正在执行的主机是不是属于主机组xx,如果是则执行debug,如果不是则不执 行。 32.1.3 when判断中is的用法 is可以用于判断变量是否被定义常见的判断包括以下3种。 1is defined变量被定义。 2is undefined等同于is not defined,变量没有被定义。 3is none变量被定义了,但是值为空。 看下面的例子。 [rootpp demo3]# cat when-4.yaml
---
- hosts: upvars:aa: 1bb:tasks:- name: tasks1debug: msg111when: aa is undefined- name: tasks2debug: msg222when: bb is undefined- name: tasks3debug: msg333when: cc is not defined
[rootpp demo3]# 首先定义了两个变量aa和 bb其中bb的值为空此处并没有定义cc。后面定义了以下 3个task。 1如果aa被定义了则显示111这里aa被定义了所以判断成立会执行task1。 2如果b没有被定义则显示222这里bb被定义了所以判断不成立不会执行 task2。 3如果cc没有被定义则显示333这里cc没有被定义所以判断成立会执行 task3。 这里is undefined 和is not defined是一个意思。 查看运行的结果,如下所示。 [rootpp demo3]# ansible-playbook when-4.yamlPLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks1] ****************************************************************************
skipping: [up]TASK [tasks2] ****************************************************************************
skipping: [up]TASK [tasks3] ****************************************************************************
ok: [up] {msg: 333
}PLAY RECAP *******************************************************************************
up : ok2 changed0 unreachable0 failed0 skipped2 rescued0 ignored0 32.2判断语句block-rescue 对于when来说只能做一个判断成立就执行不成立就不执行。block和rescue一般 同用,类似于shell判断语句中的if-else在block下面可以包含多个模块来判断这多个模块 是否执行成功了。 block-rescue的用法如下。 1 block:
2 ‐ 模块1
3 ‐ 模块2
4 ‐ 模块3
5 rescue:
6 ‐ 模块1
7 ‐ 模块2 先执行 block中的模块1如果没有报错则继续执行模块2如果block中的所有模块都 执行成功了则跳过rescue 中的所有模块直接执行下一个task中的模块如图32-1所 示。 这里有2个task : task1和 task2在 task1的block中有3个模块rescue中有2个模块。 如果 block1中的所有模块都正确执行了则不执行rescue中的模块直接执行task2。 如果 block中的任一模块执行失败block中其他后续的模块都不再执行,然后会跳转执行 rescue 中的模块如图32-2所示。 这里block1中的模块1执行完成之后会执行模块2如果模块2报错则不会执行模块3 直接跳转到rescue中执行模块x。rescue中的所有模块全部正确执行完成之后,则执行 task2。 如果rescue中的某个模块执行失败则退出整个playbook如图32-3所示。 这里 block中的模块2执行失败则跳转到rescue中执行模块x如果模块x执行失败,则退 出整个 playbook,即也不会执行task2了。 如果某个报错模块有 ignore_errors: yes选项则会忽略此模块的错误继续执行下一个 模块,如图32-4所示。 这里block中的模块2执行失败了但是因为加了ignore_errors: yes选项所以会忽略这 个报错模块,继续执行模块3。 练习1按上面的描述写一个playbook内容如下。 [rootpp demo3]# cat block-1.yaml
---
- hosts: uptasks:- name: task1block:- name: 11debug: msg1111- name: 22shell: ls /aa.txt- name: 33debug: msg3333rescue:- name: xxdebug: msgxxxx- name: yydebug: msgyyyy- name: task2debug: msgzzzz[rootpp demo3]# 这里在task1的block中运行了3个模块第一个模块可以正确执行第二个模块是执行一 个系统命令ls /aa.txt但是在server2中是不存在/aa.txt这个文件的所以这个模块会执行 失败。block中的第三个模块不再执行直接跳转到rescue中的模块。rescue中的2个模块均 可正确执行然后执行task2。 所以屏幕上会显示1111, xxxx, yyyy, zzzz。运行结果如下。 [rootpp 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 : ok5 changed0 unreachable0 failed0 skipped0 rescued1 ignored0 32.3 循环语句 在shell中 for循环的用法如下。 1 for i in A B C ... ; do
2 命令 $
3 done 这里首先把A赋值给i执行do和done之间的命令;然后把B赋值给i执行do和 done之间 的命令以此类推直到把in后面所有的值执行完毕。for后面的变量可以随便命名。 再回顾一下前面介绍的列表如下所示。 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 这里列表employee中有3个元素分别记录了lisi、wangwu、xiaohua的信息。我们把 这3个元素当成刚讲的for循环中的A、B、C。先把第一个元素赋值给变量执行某个操作 完成之后再把第二个元素赋值给变量。 用for循环A、B、C在playbook中用loop来循环列表中的元素。在for循环中,指定一个 变量如i然后分别把A、B、C赋值给i。 在loop中使用一个固定的变量 item然后把每个元素赋值给item如图32-5所示。第 二次循环如图32-6所示。 练习1定义一个列表users然后循环这个列表中的每个元素,命令如下。 [rootpp demo3]# cat loop-1.yaml
---
- hosts: upvars:users:- uname: tomage: 20sex: man- uname: bobage: 22sex: man- uname: maryage: 20sex: womantasks:- name: task1debug: msg{{ item }}loop: {{ users }}
[rootpp demo3]# 这里定义了一个列表users里面包含了3个用户的信息在taskl中用loop开始循环这个 列表。loop后面写列表名时需要使用引号引起来这里的关键字loop可以换成关键字 with_items. 这里首先把users的第一个元素赋值给item用debug 打印;然后把users的第二个元素赋 值给item,用 debug打印,直到把所有的元素都赋值给 item。 运行此 playbook命令如下。 [rootpp 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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 练习2修改loop-1.yaml的内容如下。 [rootpp demo3]# cat loop-1.yaml
---
- hosts: upvars:users:- uname: tomage: 20sex: man- uname: bobage: 22sex: man- uname: maryage: 20sex: womantasks:- name: task1debug: msg{{ item.uname }}loop: {{ users }}
[rootpp demo3]# 列表的每个元素都是一个字典所以 item就是字典要获取这个字典中的uname变量,用 item.uname即可。 运行此 playbook命令如下。 [rootpp 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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 练习3如果想打印所有性别为男的那些用户名修改loop-1.yaml 的内容如下。 [rootpp demo3]# cat loop-1.yaml
---
- hosts: upvars:users:- uname: tomage: 20sex: man- uname: bobage: 22sex: man- uname: maryage: 20sex: womantasks:- name: task1debug: msg{{ item.uname }}when: item.sex manloop: {{ users }}
[rootpp demo3]# 在此playbook中我们用when加了一个判断。循环列表时首先把第一个元素赋值给 item然后判断item.sex的值是否为man如果是则判断成立执行debug模块;如果不是 则判断不成立,不执行debug模块。 第一次循环结束之后开始第二次循环把第二个元素赋值给item之后做相同的判 断。运行此 playbook命令如下。 [rootpp demo3]# ansible-playbook loop-1.yamlPLAY [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 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 [rootpp demo3]# 这样就把所有性别为男的用户名打印出来了。