ansible笔记(22):循环(四)

栏目: 服务器 · 发布时间: 7年前

内容简介:所属分类:ansible
  • A+

所属分类:ansible 运维技术

在本博客中,ansible是一个系列文章,我们会尽量以通俗易懂的方式总结ansible的相关知识点。

ansible系列博文直达链接:ansible轻松入门系列

"ansible系列"中的每篇文章都建立在前文的基础之上,所以, 请按照顺序阅读这些文章,否则有可能在阅读中遇到障碍。

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

今天聊聊 "with_indexed_items"的用法,顾名思义,"with_indexed_items"应该与"索引"有关,没错,"with_indexed_items"的作用就是在循环处理列表时为列表中的每一项添加"数字索引","索引"从0开始,这样说可能不够直观,我们来看一个小示例,示例playbook如下:

---
- 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执行后输出的信息如下:

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如下

---
- 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如下:

---
- 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"处理这个列表,上例执行后,输出如下

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如下

---
- hosts: test70
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{ item }}"
    with_indexed_items:
    - [ test1, test2 ]
    - [ test3, [ test4, test5 ] ]
    - [ test6 ]

如上例所示,我们又在之前示例的基础上,多嵌套了一层列表,那么执行上例playbook,输出信息如下

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"的使用就总结到这里,希望能够对你有所帮助。

我的微信公众号

关注"实用运维笔记"微信公众号,当博客中有新文章时,可第一时间得知哦~


以上所述就是小编给大家介绍的《ansible笔记(22):循环(四)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Inside Larry's and Sergey's Brain

Inside Larry's and Sergey's Brain

Richard Brandt / Portfolio / 17 Sep 2009 / USD 24.95

You’ve used their products. You’ve heard about their skyrocketing wealth and “don’t be evil” business motto. But how much do you really know about Google’s founders, Larry Page and Sergey Brin? Inside......一起来看看 《Inside Larry's and Sergey's Brain》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具