黑帽联盟

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

作者: yun    时间: 2019-9-8 19:38
标题: ansible笔记(22):循环(四)
本帖最后由 yun 于 2019-9-11 17:23 编辑

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


话接前文,我们继续来聊聊关于循环的关键字。

今天聊聊 "with_indexed_items"的用法,顾名思义,"with_indexed_items"应该与"索引"有关,没错,"with_indexed_items"的作用就是在循环处理列表时为列表中的每一项添加"数字索引","索引"从0开始,这样说可能不够直观,我们来看一个小示例,示例playbook如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{ item }}"
  8.     with_indexed_items:
  9.     - test1
  10.     - test2
  11.     - test3
复制代码
上例中我们定义了一个列表,列表中有3个值,test1、test2、test3,我们使用"with_indexed_items"关键字处理这个列表,然后使用debug模块输出了item的信息,那么上例playbook执行后输出的信息如下:
  1. TASK [debug] **********************************
  2. ok: [test70] => (item=(0, u'test1')) => {
  3.     "changed": false,
  4.     "item": [
  5.         0,
  6.         "test1"
  7.     ],
  8.     "msg": [
  9.         0,
  10.         "test1"
  11.     ]
  12. }
  13. ok: [test70] => (item=(1, u'test2')) => {
  14.     "changed": false,
  15.     "item": [
  16.         1,
  17.         "test2"
  18.     ],
  19.     "msg": [
  20.         1,
  21.         "test2"
  22.     ]
  23. }
  24. ok: [test70] => (item=(2, u'test3')) => {
  25.     "changed": false,
  26.     "item": [
  27.         2,
  28.         "test3"
  29.     ],
  30.     "msg": [
  31.         2,
  32.         "test3"
  33.     ]
  34. }
复制代码
从上述输出信息的msg中可以看到,"with_indexed_items"在处理列表中的每一项时,按照顺序为每一项添加了编号,test1对应的索引编号是0,test2的编号是1,test3的编号是2,"with_indexed_items"将添加过编号的每一项放入到了item中,所以,我们可以在处理每一项的时候同时获取到对应的编号,playbook如下
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "index is : {{ item.0 }} , value is {{ item.1 }}"
  8.     with_indexed_items:
  9.     - test1
  10.     - test2
  11.     - test3
复制代码
上例中,我们已经能够通过"with_indexed_items"获取到列表中每个项的值以及对应的编号,但是,上述两个示例都是简单的单层列表,如果遇到像前文中出现的多层嵌套列表,"with_indexed_items"会怎样处理呢?我们来试试,示例playbook如下:
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "index is : {{ item.0 }} , value is {{ item.1 }}"
  8.     with_indexed_items:
  9.     - [ test1, test2 ]
  10.     - [ test3, test4, test5 ]
  11.     - [ test6, test7 ]
复制代码
如上例所示,我们定义了一个嵌套的列表,列表中的每一项又是一个小列表,我们使用"with_indexed_items"处理这个列表,上例执行后,输出如下
  1. TASK [debug] *****************************
  2. ok: [test70] => (item=(0, u'test1')) => {
  3.     "changed": false,
  4.     "item": [
  5.         0,
  6.         "test1"
  7.     ],
  8.     "msg": "index is : 0 , value is test1"
  9. }
  10. ok: [test70] => (item=(1, u'test2')) => {
  11.     "changed": false,
  12.     "item": [
  13.         1,
  14.         "test2"
  15.     ],
  16.     "msg": "index is : 1 , value is test2"
  17. }
  18. ok: [test70] => (item=(2, u'test3')) => {
  19.     "changed": false,
  20.     "item": [
  21.         2,
  22.         "test3"
  23.     ],
  24.     "msg": "index is : 2 , value is test3"
  25. }
  26. ok: [test70] => (item=(3, u'test4')) => {
  27.     "changed": false,
  28.     "item": [
  29.         3,
  30.         "test4"
  31.     ],
  32.     "msg": "index is : 3 , value is test4"
  33. }
  34. ok: [test70] => (item=(4, u'test5')) => {
  35.     "changed": false,
  36.     "item": [
  37.         4,
  38.         "test5"
  39.     ],
  40.     "msg": "index is : 4 , value is test5"
  41. }
  42. ok: [test70] => (item=(5, u'test6')) => {
  43.     "changed": false,
  44.     "item": [
  45.         5,
  46.         "test6"
  47.     ],
  48.     "msg": "index is : 5 , value is test6"
  49. }
  50. ok: [test70] => (item=(6, u'test7')) => {
  51.     "changed": false,
  52.     "item": [
  53.         6,
  54.         "test7"
  55.     ],
  56.     "msg": "index is : 6 , value is test7"
  57. }
复制代码
你目光如炬,一定发现了,当我们定义了两层的嵌套列表时,"with_indexed_items"会将嵌套的两层列表"拉平","拉平"后按照顺序为每一项编号,"拉平"效果跟之前总结的"with_flattened"效果类似(如果忘了怎样使用"with_flattened"请回顾前文),但是,当处理这种嵌套的多层列表时,"with_indexed_items"的拉平效果与"with_flattened"的完全一致么,我们再来实验一下,我们把上例的嵌套列表改的更加复杂一些,再多嵌套一层,示例playbook如下
  1. ---
  2. - hosts: test70
  3.   remote_user: root
  4.   gather_facts: no
  5.   tasks:
  6.   - debug:
  7.       msg: "{{ item }}"
  8.     with_indexed_items:
  9.     - [ test1, test2 ]
  10.     - [ test3, [ test4, test5 ] ]
  11.     - [ test6 ]
复制代码
如上例所示,我们又在之前示例的基础上,多嵌套了一层列表,那么执行上例playbook,输出信息如下
  1. TASK [debug] ********************************
  2. ok: [test70] => (item=(0, u'test1')) => {
  3.     "changed": false,
  4.     "item": [
  5.         0,
  6.         "test1"
  7.     ],
  8.     "msg": [
  9.         0,
  10.         "test1"
  11.     ]
  12. }
  13. ok: [test70] => (item=(1, u'test2')) => {
  14.     "changed": false,
  15.     "item": [
  16.         1,
  17.         "test2"
  18.     ],
  19.     "msg": [
  20.         1,
  21.         "test2"
  22.     ]
  23. }
  24. ok: [test70] => (item=(2, u'test3')) => {
  25.     "changed": false,
  26.     "item": [
  27.         2,
  28.         "test3"
  29.     ],
  30.     "msg": [
  31.         2,
  32.         "test3"
  33.     ]
  34. }
  35. ok: [test70] => (item=(3, [u'test4', u'test5'])) => {
  36.     "changed": false,
  37.     "item": [
  38.         3,
  39.         [
  40.             "test4",
  41.             "test5"
  42.         ]
  43.     ],
  44.     "msg": [
  45.         3,
  46.         [
  47.             "test4",
  48.             "test5"
  49.         ]
  50.     ]
  51. }
  52. ok: [test70] => (item=(4, u'test6')) => {
  53.     "changed": false,
  54.     "item": [
  55.         4,
  56.         "test6"
  57.     ],
  58.     "msg": [
  59.         4,
  60.         "test6"
  61.     ]
  62. }
复制代码
你肯定看出了问题所在,没错,当多加了一层嵌套以后,"with_indexed_items"并不能像"with_flattened"一样将嵌套的列表"完全拉平",第二层列表中的项如果仍然是一个列表,"with_indexed_items"则不会拉平这个列表,而是将其当做一个整体进行编号。

关于"with_indexed_items"的使用就总结到这里,希望能够对你有所帮助。






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