循环访问ansible数组

时间:2019-02-04 18:08:35

标签: ansible

我正在使用ansible任务注册两个变量,其结果是-

"pathlist": [
        "/dir1/dir2/dir3",
        "/dir1/dir2/dir3/abc.txt",
        "/dir1/dir2/dir3/testme"
    ]

"pathexists": [
        true,
        true,
        false
    ]

我想将输出打印为-

"/dir1/dir2/dir3" -- true
"/dir1/dir2/dir3/abc.txt" -- true
"/dir1/dir2/dir3/testme" -- false

我正在使用以下代码来打印最终结果- -set_fact:
    输出:“ {{item.0}}-{{item.1}} \ n”
  with_nested:
    -“ {{pathlist}}”     -“ {{pathexists}}”

  • 调试:var = output

我想,我缺少了一些东西。任何帮助都会很有用。

1 个答案:

答案 0 :(得分:0)

with_together 是您要寻找的

 tasks:
   - debug: msg="{{ item.0 }} -- {{ item.1 }}"
     with_together:
       - "{{ pathlist }}"
       - "{{ pathexists }}"

给予:

    "msg": "/dir1/dir2/dir3 -- True"
    "msg": "/dir1/dir2/dir3/abc.txt -- True"
    "msg": "/dir1/dir2/dir3/testme -- False"