How to loop over this dictionary in Ansible?

前端 未结 5 595
说谎
说谎 2020-12-02 22:59

Say I have this dictionary

war_files:
  server1:
  - file1.war
  - file2.war
  server2:
  - file1.war
  - file2.war
  - file3.war

and for n

5条回答
  •  北海茫月
    2020-12-02 23:50

    Hows this

    - hosts: localhost
      vars:
        war_files:
          server1:
          - file1.war
          - file2.war
          server2:
          - file1.war
          - file2.war
          - file3.war
      tasks:
        - name: Loop over subelements of the dictionary
          debug:
            msg: "Key={{ item.0.key }} value={{ item.1 }}"
          loop: "{{ war_files | dict2items | subelements('value') }}"
    

    dict2items, subelements filters are coming in Ansible 2.6.

    FYI, if a filter for your objective doesn't exist, you can write your own in python without having to resort to jinja2 hacks. Ansible is easily extendable; filters in filter_plugins/*.py are searched by default adjacent to your plays/roles and are automatically included - see Developing Plugins for details.

提交回复
热议问题