How to loop over this dictionary in Ansible?

前端 未结 5 528
说谎
说谎 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:39

    dict2items

    I found myself wanting to iterate over a heterogeneous set of keys and their associated values and use the key-value pair in a task. The dict2items filter is the least painful way I've found. You can find dict2items in Ansible 2.6

    Example Dict

    systemsetup:
      remotelogin: "On"
      timezone: "Europe/Oslo"
      usingnetworktime: "On"
      sleep: 0
      computersleep: 0
      displaysleep: 0
      harddisksleep: 0
      allowpowerbuttontosleepcomputer: "Off"
      wakeonnetworkaccess: "On"
      restartfreeze: "On"
      restartpowerfailure: "On"
    

    Example Task

    ---
    - debug:
        msg: "KEY: {{ item.key }}, VALUE: {{ item.value }}"
      loop: "{{ systemsetup | dict2items }}"
    
    0 讨论(0)
  • 2020-12-02 23:40

    Now Ansible allows this

    - name: add several users
      user:
        name: "{{ item.name }}"
        state: present
        groups: "{{ item.groups }}"
      with_items:
        - { name: 'testuser1', groups: 'wheel' }
        - { name: 'testuser2', groups: 'root' }
    
    0 讨论(0)
  • 2020-12-02 23:46

    Here is my preferred way to loop over dictionaries:

    input_data.yml contains the following:

    ----
    input_data:
      item_1:
        id: 1
        info: "Info field number 1"
      item_2:
        id: 2
        info: "Info field number 2"
    

    I then use a data structure like the above in a play using the keys() function and iterate over the data using with_items:

    ---
    - hosts: localhost
      gather_facts: false
      connection: local
      tasks:
        - name: Include dictionary data
          include_vars:
            file: data.yml
    
        - name: Show info field from data.yml
          debug:
            msg: "Id: {{ input_data[item]['id'] }} - info: {{ input_data[item]['info'] }}"
          with_items: "{{ input_data.keys() | list }}"
    

    The above playbook produces the following output:

    PLAY [localhost] ***********************************************************
    
    TASK [Include dictionary data] *********************************************
    ok: [localhost]
    
    TASK [Show info field from data.yml] ***************************************
    ok: [localhost] => (item=item_2) => {
        "msg": "Id: 2 - info: Info field item 2"
    }
    ok: [localhost] => (item=item_3) => {
        "msg": "Id: 3 - info: Info field item 3"
    }
    ok: [localhost] => (item=item_1) => {
        "msg": "Id: 1 - info: Info field item 1"
    }
    
    PLAY RECAP *****************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 23:59

    EDIT: At the time of writing this answer, Ansible 2.6 wasn't out. Please read the answer provided by @tmoschou, as it is much better.


    Well, I couldn't find a very easy way to do it, however, with a little bit of jinja2, we can achieve something of this sort:

    /tmp ❯❯❯ cat example.yml
    ---
    - hosts: 127.0.0.1
      vars:
        war_files:
          server1:
          - file1.war
          - file2.war
          server2:
          - file1.war
          - file2.war
          - file3.war
      tasks:
      - set_fact:
          war_files_list_of_dicts: |
              {% set res = [] -%}
              {% for key in war_files.keys() -%}
                 {% for value in war_files[key] -%}
                  {% set ignored = res.extend([{'Server': key, 'WAR':value}]) -%}
                 {%- endfor %}
              {%- endfor %}
              {{ res }}
    
      - name: let's debug the crap out of this
        debug: var=war_files_list_of_dicts
    
      - name: Servers and their WARs!!!
        debug:
           msg: "Server={{ item.Server }}, WAR={{ item.WAR }}"
        with_items: "{{ war_files_list_of_dicts }}"
    

    And, when the playbook is run:

    /tmp ❯❯❯ ansible-playbook example.yml
     [WARNING]: provided hosts list is empty, only localhost is available
    
    
    PLAY [127.0.0.1] ***************************************************************
    
    TASK [setup] *******************************************************************
    ok: [127.0.0.1]
    
    TASK [set_fact] ****************************************************************
    ok: [127.0.0.1]
    
    TASK [let's debug the crap out of this] ****************************************
    ok: [127.0.0.1] => {
        "war_files_list_of_dicts": [
            {
                "Server": "server1", 
                "WAR": "file1.war"
            }, 
            {
                "Server": "server1", 
                "WAR": "file2.war"
            }, 
            {
                "Server": "server2", 
                "WAR": "file1.war"
            }, 
            {
                "Server": "server2", 
                "WAR": "file2.war"
            }, 
            {
                "Server": "server2", 
                "WAR": "file3.war"
            }
        ]
    }
    
    TASK [Servers and their WARs!!!] ***********************************************
    ok: [127.0.0.1] => (item={'WAR': u'file1.war', 'Server': u'server1'}) => {
        "item": {
            "Server": "server1", 
            "WAR": "file1.war"
        }, 
        "msg": "Server=server1, WAR=file1.war"
    }
    ok: [127.0.0.1] => (item={'WAR': u'file2.war', 'Server': u'server1'}) => {
        "item": {
            "Server": "server1", 
            "WAR": "file2.war"
        }, 
        "msg": "Server=server1, WAR=file2.war"
    }
    ok: [127.0.0.1] => (item={'WAR': u'file1.war', 'Server': u'server2'}) => {
        "item": {
            "Server": "server2", 
            "WAR": "file1.war"
        }, 
        "msg": "Server=server2, WAR=file1.war"
    }
    ok: [127.0.0.1] => (item={'WAR': u'file2.war', 'Server': u'server2'}) => {
        "item": {
            "Server": "server2", 
            "WAR": "file2.war"
        }, 
        "msg": "Server=server2, WAR=file2.war"
    }
    ok: [127.0.0.1] => (item={'WAR': u'file3.war', 'Server': u'server2'}) => {
        "item": {
            "Server": "server2", 
            "WAR": "file3.war"
        }, 
        "msg": "Server=server2, WAR=file3.war"
    }
    
    PLAY RECAP *********************************************************************
    127.0.0.1                  : ok=4    changed=0    unreachable=0    failed=0   
    
    0 讨论(0)
提交回复
热议问题