linux中playbook的控制语句

本章主要介绍 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拷贝进去,命令如下。
[root@pp ~]# mkdir demo3
[root@pp ~]# cp ansible.cfg hosts demo3/
[root@pp ~]# cd demo3/
[root@pp demo3]# 
32.1.1 when判断中>、<、!=的使用
练习1:写一个playbook,判断某条件是否成立,成立了才执行task,否则不执行,命令
如下。
[root@pp demo3]# cat when-1.yaml 
---
- hosts: up
  tasks:
  - name: tasks1
    debug: msg="111"
    when: 1<2
[root@pp demo3]# 
这里有一个task,判断1<2是否成立,如果成立则执行task1,屏幕上会显示111;如果不
成立则不执行taskl,屏幕上不会显示111。这里明显是成立的,所以会执行task1。运行结
果如下。
[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   

hen后面可以有多个条件,用or或and作为连接符。
如果用or作为连接符,只要有一个条件成立即可,只有所有的条件都不成立时,整体才不成
立。
练习2:修改when-1.yaml的内容如下。
[root@pp demo3]# cat when-1.yaml 
---
- hosts: up
  tasks:
  - name: tasks1
    debug: msg="111"
    when: 1<2 or 2>3
此处用or作为连接符,只要有一个条件成立就会成立,2>3不成立,但是1<2成立,所以
整体上就是成立的。运行结果如下。
[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   

常见的判断符包括以下6种。
(1)==:等于。
(2)!=:不等于。
(3)>:大于。
(4)>=:大于等于。
(5)<:小于。
(6)<=:小于等于。
练习:如果server2的系统主版本是7(RHEL/CentOS7),则打印111,否则不打印。
playbook的内容如下。
[root@pp demo3]# cat when-2.yaml 
---
- hosts: up
  tasks:
  - name: tasks2
    debug: msg="111"
    when: ansible_distribution_major_version == "7"
[root@pp demo3]# 
因为server2的系统是RHEL8,所以不会执行此task2,即不会显示111。
[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   

注意
ansible_distribution major version的值是一个字符串,所以when判断中=后面的7是要加引号
的。
练习2:修改when-2.yaml 的内容如下。
[root@pp demo3]# cat when-2.yaml 
---
- hosts: up
  tasks:
  - name: tasks2
    debug: msg="111"
    when: ansible_distribution_major_version == "8"
[root@pp demo3]# 
再次运行此playbook,命令如下,会显示111。
[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]# 
再次提醒:在when 中引用变量时是不用加{{}}的。
32.1.2 when判断中in的用法
在when语句中,除可以使用上面的大于、小于等判断方法外,还可以使用 in,用法如
下。
1 value in 列表
如果此值在这个列表中,则判断成立,否则不成立。
练习:判断某值是否在列表中,编写 when-3.yaml,命令如下。
[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]# 
此处定义了一个列表 list1,里面有4个值,分别为1、2、3、4;定义了一个task打印333,
会不会执行这个task,就要看when后面的判断是否成立。如果2在列表list1中,则执行;如果
不在,则不执行,很明显2在列表list1中,所以会执行此task,即屏幕上会显示333。运行结
果如下。
[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 
因为2在列表list1中,when判断成立,可以正确执行task3,所以屏幕上会显示333。修
改when-3.yaml的内容如下。
[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]# 
这里判断的是2不在列表list1中,但2是在列表list1中的,所以判断不成立。运行结果如
下。
[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]# 
因为when判断不成立,所以屏幕上不会显示333。回想前面的例子。
[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]# 
这里判断当前正在执行的主机是不是属于主机组xx,如果是则执行debug,如果不是则不执
行。
32.1.3 when判断中is的用法
is可以用于判断变量是否被定义,常见的判断包括以下3种。
(1)is defined:变量被定义。
(2)is undefined:等同于is not defined,变量没有被定义。
(3)is none:变量被定义了,但是值为空。
看下面的例子。
[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]# 
首先定义了两个变量: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是一个意思。
查看运行的结果,如下所示。
[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   

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,内容如下。
[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]# 
这里在task1的block中运行了3个模块,第一个模块可以正确执行,第二个模块是执行一
个系统命令ls /aa.txt,但是在server2中是不存在/aa.txt这个文件的,所以这个模块会执行
失败。block中的第三个模块不再执行,直接跳转到rescue中的模块。rescue中的2个模块均
可正确执行,然后执行task2。
所以,屏幕上会显示1111, xxxx, yyyy, zzzz。运行结果如下。
[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 循环语句

在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,然后循环这个列表中的每个元素,命令如下。
[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]# 
这里定义了一个列表users,里面包含了3个用户的信息,在taskl中用loop开始循环这个
列表。loop后面写列表名时,需要使用引号引起来,这里的关键字loop可以换成关键字
with_items.
这里首先把users的第一个元素赋值给item,用debug 打印;然后把users的第二个元素赋
值给item,用 debug打印,直到把所有的元素都赋值给 item。
运行此 playbook,命令如下。
[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   

练习2:修改loop-1.yaml的内容如下。
[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]# 
列表的每个元素都是一个字典,所以 item就是字典,要获取这个字典中的uname变量,用
item.uname即可。
运行此 playbook,命令如下。
[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  
练习3:如果想打印所有性别为男的那些用户名,修改loop-1.yaml 的内容如下。
[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]# 
在此playbook中,我们用when加了一个判断。循环列表时,首先把第一个元素赋值给
item,然后判断item.sex的值是否为man,如果是则判断成立,执行debug模块;如果不是 则判断不成立,不执行debug模块。
第一次循环结束之后,开始第二次循环,把第二个元素赋值给item之后,做相同的判
断。运行此 playbook,命令如下。
[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]# 
这样就把所有性别为男的用户名打印出来了。
  • 18
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值