运维开发网

ansible循环四

运维开发网 https://www.qedev.com 2020-09-08 14:10 出处:51CTO 作者:菜机运维
with_indexed_items示例:hosts:jack6_1remote_user:rootgather_facts:notasks:debug:msg:"{{item}}"with_indexed_items:[t1,t2][t3,[t4,t5]][t6]输出结果如下:[[email protected]]#ansible-playbook--syntax-checki

with_indexed_items

示例:


  • hosts: jack6_1

    remote_user: root

    gather_facts: no

    tasks:

    • debug:

      msg: "{{item}}"

      with_indexed_items:

      • [t1,t2]
      • [t3,[t4,t5]]
      • [t6]

输出结果如下:

[[email protected] work]# ansible-playbook --syntax-check item_index.yml

playbook: item_index.yml

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

PLAY [jack6_1] *****

TASK [debug] ***

ok: [jack6_1] => (item=[0, u't1']) => {

"msg": [

0,

"t1"

]

}

ok: [jack6_1] => (item=[1, u't2']) => {

"msg": [

1,

"t2"

]

}

ok: [jack6_1] => (item=[2, u't3']) => {

"msg": [

2,

"t3"

]

}

ok: [jack6_1] => (item=[3, [u't4', u't5']]) => {

"msg": [

3,

[

"t4",

"t5"

]

]

}

ok: [jack6_1] => (item=[4, u't6']) => {

"msg": [

4,

"t6"

]

}

PLAY RECAP *****

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

扫码领视频副本.gif

0

精彩评论

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

关注公众号