黑帽联盟

标题: ansible笔记(35):循环(八) [打印本页]

作者: yun    时间: 2019-9-10 14:20
标题: ansible笔记(35):循环(八)
本帖最后由 yun 于 2019-9-11 17:16 编辑

ansible是一个系列文章,我们会尽量以通俗易懂的方式总结ansible的相关知识点。
ansible系列博文直达链接:ansible轻松入门系列
"ansible系列"中的每篇文章都建立在前文的基础之上,所以,请按照顺序阅读这些文章,否则有可能在阅读中遇到障碍。


上一篇文章中我们提到过,ansible官网推荐了新的方式操作循环,那么这篇文章中,我就就来聊聊这种新的方式,以便能够更好的从老版本的使用习惯过渡过来。

在2.5版本之前的ansible中,大多数人习惯使用"with_X"风格的关键字操作循环,从2.6版本开始,官方开始推荐使用"loop"关键字代替"with_X"风格的关键字,我们先来看一个小示例,使用loop关键字进行最简单的列表循环,示例如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{ item }}"
  8.     loop:
  9.     - teststr1
  10.     - teststr2
复制代码
上例使用"loop"关键字,替换了之前总结的"with_list"这种"with_X"风格的关键字,它们的效果是完全相同的。

在总结lookup插件的用法时,已经详细的描述过,我们可以使用"loop"关键字配合对应的"lookup"插件,替换更多的、具有更复杂功能的"with_X"风格的关键字,比如,使用loop关键字和dict插件替换"with_dict"关键字,示例如下
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   vars:
  6.     users:
  7.       alice: female
  8.       bob: male
  9.   tasks:
  10.   - debug:
  11.       msg: "{{item.key}} is {{item.value}}"
  12.     loop: "{{ lookup('dict',users) }}"
复制代码
这个示例在上一篇文章中已经解释过,此处不再赘述。

上例使用了"loop加lookup"的方式,完成了循环操作,而在2.6版本的官网手册中,官方开始推荐使用"loop加filter"的方式来替代"loop加lookup"的方式,什么意思呢?我们来看一个小例子,如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   vars:
  6.     users:
  7.       alice: female
  8.       bob: male
  9.   tasks:
  10.   - debug:
  11.       msg: "{{item.key}} is {{item.value}}"
  12.     loop: "{{ users | dict2items }}"
复制代码
如上例所示,在使用loop关键字操作对应的字典变量users时,并没有借助dict插件,而是借助了一个名为dict2items的过滤器,前文已经总结了过滤器的使用方法,但是并没有介绍dict2items过滤器的用法,那么此处正好介绍一下dict2items过滤器的使用方法,dict2items过滤器是在2.6版本中新加入的过滤器,它可以把一个字典格式的数据进行转换处理,具体会进行怎样的转换处理呢?我们来看个小示例,如下
  1. users是一个字典格式的变量,它的结构是这样的
  2.     users:
  3.       alice: female
  4.       bob: male
  5. 当users字典被dict2items转换处理以后,会变成如下模样
  6.     users:
  7.     - key: alice
  8.       value: female
  9.     - key: bob
  10.       value: male
复制代码
这就是dict2items过滤器的作用,看到上述转换后的数据格式,你是不是觉得似曾相识?
没错,字典数据经过"dict2items"处理后,与字典数据经过"with_dict"处理后的格式完全相同(可以参考前文的"with_dict"总结),

经过上述描述,你应该已经明白了,无论是"with_X"、"loop加lookup"还是"loop加filter",都是使用不同的方式,实现相同的功能而已,只是在2.6版本的ansible中,官方开始推荐使用"loop加filter"的方式操作循环。

那么我们就来总结一下这种新的使用方式,由于之前已经总结过各种"with_X"关键字的使用方法,所以此处直接列出各种"with_x"关键字对应的新的使用方式。

with_list
  1. #loop可以替代with_list,当处理嵌套的列表时,列表不会被拉平
  2. ---
  3. - hosts: test70
  4.   remote_user: root
  5.   gather_facts: no
  6.   vars:
  7.     testlist:
  8.     - a
  9.     - [b,c]
  10.     - d
  11.   tasks:
  12.   - debug:
  13.       msg: "{{item}}"
  14.     loop: "{{testlist}}"
复制代码
with_flattened
  1. #flatten过滤器可以替代with_flattened,当处理多层嵌套的列表时,列表中所有的嵌套层级都会被拉平
  2. #示例如下,flatten过滤器的用法在前文中已经总结过,此处不再赘述
  3. ---
  4. - hosts: test70
  5.   remote_user: root
  6.   gather_facts: no
  7.   vars:
  8.     testlist:
  9.     - a
  10.     - [b,c]
  11.     - d
  12.   tasks:
  13.   - debug:
  14.       msg: "{{item}}"
  15.     loop: "{{testlist | flatten}}"
复制代码
with_items
  1. #flatten过滤器(加参数)可以替代with_items,当处理多层嵌套的列表时,只有列表中的第一层会被拉平
  2. ---
  3. - hosts: test70
  4.   remote_user: root
  5.   gather_facts: no
  6.   vars:
  7.     testlist:
  8.     - a
  9.     - [b,c]
  10.     - d
  11.   tasks:
  12.   - debug:
  13.       msg: "{{item}}"
  14.     loop: "{{testlist | flatten(levels=1)}}"
复制代码
直到当前版本的ansible(2.6.2版---2.6.5版),当flatten过滤器配置leves参数处理多层列表时,存在bug
但是为了尽快熟悉新的使用方式,我们暂且忽略这个bug,具体bug可以参考如下链接
https://github.com/ansible/ansible/issues/46343

with_indexed_items
  1. #flatten过滤器(加参数),再配合loop_control关键字,可以替代with_indexed_items
  2. #当处理多层嵌套的列表时,只有列表中的第一层会被拉平,flatten过滤器的bug暂且忽略
  3. #示例如下,之后会对示例进行解释
  4. ---
  5. - hosts: test70
  6.   remote_user: root
  7.   gather_facts: no
  8.   vars:
  9.     testlist:
  10.     - a
  11.     - [b,c]
  12.     - d
  13.   tasks:
  14.   - debug:
  15.       msg: "{{index}}--{{item}}"
  16.     loop: "{{testlist | flatten(levels=1)}}"
  17.     loop_control:
  18.       index_var: index
复制代码
"loop_control"关键字可以用于控制循环的行为,比如在循环时获取到元素的索引。
"index_var"是"loop_control"的一个设置选项,"index_var"的作用是让我们指定一个变量,"loop_control"会将列表元素的索引值存放到这个指定的变量中,比如如下配置
  1.   loop_control:
  2.     index_var: my_idx
复制代码
上述设置表示,在遍历列表时,当前被遍历元素的索引会被放置到"my_idx"变量中,也就是说,当进行循环操作时,只要获取到"my_idx"变量的值,就能获取到当前元素的索引值,当然,除了"index_var"选项,"loop_control"还有一些其他的选项可以使用,我们之后再进行总结。

with_together
  1. #zip_longest过滤器配合list过滤器,可以替代with_together
  2. ---
  3. - hosts: test70
  4.   remote_user: root
  5.   gather_facts: no
  6.   vars:
  7.     testlist1: [ a, b ]
  8.     testlist2: [ 1, 2, 3 ]
  9.     testlist3: [ A, B, C, D ]
  10.   tasks:
  11.   - debug:
  12.       msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"
  13.     with_together:
  14.     - "{{testlist1}}"
  15.     - "{{testlist2}}"
  16.     - "{{testlist3}}"
  17.   - debug:
  18.       msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"
  19.     loop: "{{ testlist1 | zip_longest(testlist2,testlist3) | list }}"
复制代码
上例同时写出了"with_together"和对应的新方式的使用方法,以便进行对比。
当多个列表使用"with_together"进行对齐合并时,如果多个列表的长度不同,则使用最长的列表长度进行对齐,由于短列表中的元素数量不够,所以使用"空值"与长列表中的元素进行对齐,zip_longest过滤器也会像"with_together"一样,对列表进行组合,但是还需要借助list过滤器,将组合后的数据列表化,list过滤器的用法已经总结过,如果你忘了,可以回顾前文,当使用zip_longest过滤器代替with_together关键字时,默认也是使用"空值"与长列表中的元素进行对齐,但是也可以指定其他字符串代替"空值",示例如下,表示使用"NoEle"代替"空值",与长列表中的更多的元素进行对齐。
  1.   - debug:
  2.       msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"
  3.     loop: "{{ testlist1 | zip_longest(testlist2,testlist3,fillvalue='NoEle') | list }}"
复制代码
从zip_longest过滤器的名字就可以看出,这个过滤器也是使用最长的列表长度进行对齐的,当多个列表的长度不同时,能不能使用最短的列表长度进行对齐呢?没问题,我们只需要借助另一个过滤器即可,这个过滤器的名字是"zip",示例如下
  1.   - debug:
  2.       msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"
  3.     loop: "{{ testlist1 | zip(testlist2,testlist3) | list }}"
复制代码
zip_longest和zip过滤器在2.3以及以后的版本中可用。

with_nested/with_cartesian
  1. #product过滤器配合list过滤器,可以替代with_nested和with_cartesian
  2. #如果你忘了with_nested和with_cartesian的用法,可以回顾前文
  3. ---
  4. - hosts: test70
  5.   remote_user: root
  6.   gather_facts: no
  7.   vars:
  8.     testlist1: [ a, b, c ]
  9.     testlist2: [ 1, 2, 3, 4 ]
  10.   tasks:
  11.   - debug:
  12.       msg: "{{ item.0 }}--{{ item.1 }}"
  13.     loop: "{{ testlist1 | product(testlist2) | list }}"
复制代码
product过滤器也需要将组合后的数据进行列表化,所以需要借助list过滤器


with_sequence
  1. #range过滤器配合list过滤器可以代替with_sequence
  2. #你可以先回顾一下with_sequence的用法,然后再测试如下示例
  3. ---
  4. - hosts: test70
  5.   remote_user: root
  6.   gather_facts: no
  7.   tasks:
  8.   - debug:
  9.       msg: "{{item}}"
  10.     loop: "{{ range(0, 6, 2) | list }}"
复制代码
上例表示生成数字,数字从0开始,到6结束,步长为2,但是rangge函数的操作范围不会包含结束范围,也就是说不会包含6,换句话说就是,上例会生成0、2、4三个数字,而不会包含6,在总结with_sequence时我们提到过,with_sequence还有格式化的功能,比如如下示例
  1.   - debug:
  2.       msg: "{{item}}"
  3.     with_sequence: start=2 end=6 stride=2 format="number is %0.2f"
复制代码
如果你想要使用新的方式实现上例的效果,还需要配合format过滤器一起使用,示例如下
  1.   - debug:
  2.       msg: "{{ 'number is %0.2f' | format(item) }}"
  3.     loop: "{{ range(2, 7, 2) | list }}"
复制代码
with_random_choice
  1. #使用random函数可以替代with_random_choice,由于random函数是随机取出列表中的一个值,并不涉及循环操作,所以并不用使用loop关键字。
  2. ---
  3. - hosts: test70
  4.   remote_user: root
  5.   gather_facts: no
  6.   vars:
  7.     testlist: [ a, b, c ]
  8.   tasks:
  9.   - debug:
  10.       msg: "{{ testlist | random }}"
复制代码
with_dict
  1. #除了上文总结的dict2items过滤器,dictsort过滤器也可以替代with_dict
  2. ---
  3. - hosts: test70
  4.   remote_user: root
  5.   gather_facts: no
  6.   vars:
  7.     users:
  8.       d: daisy
  9.       c: carol
  10.       a: alice
  11.       b: bob
  12.       e: ella
  13.   tasks:
  14.   - debug:
  15.       msg: "{{item.key}} -- {{item.value}}"
  16.     loop: "{{ users | dict2items }}"
  17.   - debug:
  18.       msg: "{{item.0}} -- {{item.1}}"
  19.     loop: "{{ users | dictsort }}"
复制代码
正如dictsort的名字一样,dictsort具有排序功能,dictsort会根据键名的字母顺序进行排序

with_subelements
  1. #subelements过滤器可以替代with_subelements
  2. ---
  3. - hosts: test70
  4.   remote_user: root
  5.   gather_facts: no
  6.   vars:
  7.     users:
  8.     - name: bob
  9.       gender: male
  10.       hobby:
  11.         - Skateboard
  12.         - VideoGame
  13.     - name: alice
  14.       gender: female
  15.       hobby:
  16.         - Music
  17.   tasks:
  18.   - debug:
  19.       msg: "{{item.0.name}}'s hobby is {{item.1}}"
  20.     with_subelements:
  21.     - "{{users}}"
  22.     - hobby
  23.   - debug:
  24.       msg: "{{item.0.name}}'s hobby is {{item.1}}"
  25.     loop: "{{users | subelements('hobby')}}"
复制代码
  参考之前总结的with_subelements的用法,会更有利于理解上述示例

loop_control
在介绍使用新的方式替换"with_indexed_items"时,我们已经初步的接触到了loop_control关键字,它可以用于控制循环的行为,比如,使用loop_control的index_var选项,就能在遍历列表时,将元素对应的索引写入到指定的变量中,除了index_var选项,loop_control还有一些其他的选项可用,此处我们就来总结一下这些选项。

pause选项
pause选项能够让我们设置每次循环之后的暂停时间,以秒为单位,换句话说就是设置每次循环之间的间隔时间,示例如下
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{item}}"
  8.     loop:
  9.     - 1
  10.     - 2
  11.     - 3
  12.     loop_control:
  13.       pause: 10
复制代码
上例表示每次循环之间间隔10秒


label选项
我们先不解释label选项的作用,我们先来看一个小示例,如下
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   vars:
  6.     users:
  7.       alice:
  8.         name: Alice Appleworth
  9.         gender: female
  10.         telephone: 123-456-7890
  11.       bob:
  12.         name: Bob Bananarama
  13.         gender: male
  14.         telephone: 987-654-3210
  15.   tasks:
  16.   - debug:
  17.       msg: "{{item.key}}"
  18.     loop: "{{users | dict2items}}"
复制代码
执行上例playbook,执行结果如下
1.png

从上图可以看出,我们真正需要的部分就是上图中红线标注的部分,也就是debug模块输出的msg信息,上图中蓝线标注的部分我们可能并不关注,但是当我们使用debug模块输出msg信息时,无论我们是否需要获取整个item的信息,debug模块都会将item的整个信息显示出来,当我们处理的数据越复杂,显示的item的信息就越多,显示在屏幕上的、我们不关注的信息就会越多,怎样改善这种情况呢?借助label选项就行了,来看一个小示例:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   vars:
  6.     users:
  7.       alice:
  8.         name: Alice Appleworth
  9.         gender: female
  10.         telephone: 123-456-7890
  11.       bob:
  12.         name: Bob Bananarama
  13.         gender: male
  14.         telephone: 987-654-3210
  15.   tasks:
  16.   - debug:
  17.       msg: "{{item.key}}"
  18.     loop: "{{users | dict2items}}"
  19.     loop_control:
  20.       label: "{{item.key}}"
复制代码
上述示例与之前的示例的不同之处在于使用了loop_control关键字,并且使用了label选项,label的值为item.key,与msg的值完全相同,那么,执行上例playbook,结果如下
2.png

正如你所看到的,整个输出信息变得简洁了,item的信息并没有完全显示出来,而是只显示出了item.key的值,达到了我们想要的效果,这就是label选项的作用,它可以在循环输出信息时,简化输出item的信息。


loop_var选项
loop_var选项的作用我们暂且先不进行介绍,因为如果想要搞明白loop_var选项的作用,最好先搞明白"include_tasks"的用法,但是由于到目前为止,我们还没有总结include的相关用法,所以此处暂且放下,等到我们总结include的用法时,再顺势介绍loop_var选项的作用,会更加事半功倍的让我们去理解它。


这篇文章就总结到这里,希望能够对你有所帮助~~





欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5