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

2019年7月29日 下午6:04 沙发
大佬,我写的debug为什么不显示item,只有msg
ok: [ansible] => (item=[0, ‘test1’]) => {
“msg”: [
0,
“test1”
]
}
ok: [ansible] => (item=[1, ‘test2’]) => {
“msg”: [
1,
“test2”
]
}
ok: [ansible] => (item=[2, ‘test3’]) => {
“msg”: [
2,
“test3”
]
}
2018年10月19日 下午11:01 板凳
打卡
2018年10月19日 下午11:00 地板
我就靠兄弟的博客学习,跳槽
2018年8月30日 上午8:32 4楼
打卡
2018年7月20日 下午4:15 5楼
朱哥哥,还没更新完吗?
2018年7月20日 下午4:46 1层
@赛里 兄弟是想快点看完结篇还是想快更完这个然后更别的
2018年7月20日 下午10:09 2层
@朱双印 哈哈,我是想看完,然后学习哥哥的puppet。膜拜你,感谢分享。