运维开发网

ansible的循环二

运维开发网 https://www.qedev.com 2020-09-08 14:00 出处:51CTO 作者:菜机运维
with_items遍历列表中每个元素,包括嵌套列表with_list将嵌套列表作为整体元素遍历with_together将多个列表中的子列表元素,一起输出,不成对则null补位示例:hosts:jack6_1remote_user:rootgather_facts:notasks:debug:msg:"{{item}}"with_items:[1,2,3][a,b]debug

with_items 遍历列表中每个元素,包括嵌套列表

with_list 将嵌套列表作为整体元素遍历

with_together 将多个列表中的子列表元素,一起输出,不成对则null补位

示例:


  • hosts: jack6_1

    remote_user: root

    gather_facts: no

    tasks:

    • debug:

      msg: "{{item}}"

      with_items:

      • [1,2,3]
      • [a,b]
    • debug:

      msg: "{{item}}"

      with_list:

      • [4,5,6]
      • [c,d]
    • debug:

      msg: "{{item}}"

      with_together:

      • [7,8,9]
      • [e,f]

输出结果如下:

[[email protected] work]# ansible-playbook item_list.yml

PLAY [jack6_1] *****

TASK [debug] ***

ok: [jack6_1] => (item=1) => {

"msg": 1

}

ok: [jack6_1] => (item=2) => {

"msg": 2

}

ok: [jack6_1] => (item=3) => {

"msg": 3

}

ok: [jack6_1] => (item=a) => {

"msg": "a"

}

ok: [jack6_1] => (item=b) => {

"msg": "b"

}

TASK [debug] ***

ok: [jack6_1] => (item=[4, 5, 6]) => {

"msg": [

4,

5,

6

]

}

ok: [jack6_1] => (item=[u'c', u'd']) => {

"msg": [

"c",

"d"

]

}

TASK [debug] ***

ok: [jack6_1] => (item=[7, u'e']) => {

"msg": [

7,

"e"

]

}

ok: [jack6_1] => (item=[8, u'f']) => {

"msg": [

8,

"f"

]

}

ok: [jack6_1] => (item=[9, None]) => {

"msg": [

9,

null

]

}

PLAY RECAP *****

jack6_1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号