在Ansible中使用嵌套的with_items任务

时间:2020-06-11 09:31:19

标签: ansible

我在ansible中有2个with_item任务,我想将这两个任务结合起来并使用嵌套的项。

我想做这样的事情。

    - debug: msg="{{ item }}"
      with_items:
        - "{{ IP.split(',') }}"

    - debug: msg="{{ item.json.Password }}"
      with_items:
        - "{{ password.results }}"

    - debug: msg="{{ item.0 }} {{ item.1 }}"
      with_nested:
        - [ "{{ IP.split(',') }}" ]
        - [ "{{ password.results.json.Password }}" ]

前两个任务正在成功运行。但是第三项错误为

"fatal: [localhost]: FAILED! => {"msg": "'list object' has no attribute 'json'"}"

1 个答案:

答案 0 :(得分:1)

尝试

  with_nested:
    - "{{ IP.split(',') }}"
    - "{{ password.results|json_query('[].json.Password') }}"

Q:错误“您需要安装jmespath”

A:也可以使用过滤器 map 。例如

  with_nested:
    - "{{ IP.split(',') }}"
    - "{{ password.results|map(attribute='json.Password')|list }}"
相关问题