Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop over this dictionary in Ansible?

Say I have this dictionary

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

and for now I just want to loop over each item (key), and then over each item in the key (value). I did this

- name: Loop over the dictionary   debug: msg="Key={{ item.key }} value={{ item.value }}"   with_dict: "{{ war_files }}" 

And I get this. It is of course correct, but is NOT what I want.

ok: [localhost] => (item={'value': [u'file1.war', u'file2.war'], 'key': u'server1'}) => {     "item": {         "key": "server1",          "value": [             "file1.war",              "file2.war"         ]     },      "msg": "Server=server1, WAR=[u'file1.war', u'file2.war']" } ok: [localhost] => (item={'value': [u'file1.war', u'file2.war', u'file3.war'], 'key': u'server2'}) => {     "item": {         "key": "server2",          "value": [             "file1.war",              "file2.war",              "file3.war"         ]     },      "msg": "Server=server2, WAR=[u'file1.war', u'file2.war', u'file3.war']" } 

I want to get an output that says

"msg": "Server=server1, WAR=file1.war" "msg": "Server=server1, WAR=file2.war" "msg": "Server=server2, WAR=file1.war" "msg": "Server=server2, WAR=file2.war" "msg": "Server=server2, WAR=file3.war" 

IOW, how can I write a task to iterates over the dictionary so it goes through each key, and then the items within each key? In essence, I have a nested array and want to iterate over it?

like image 480
Chris F Avatar asked Feb 10 '17 20:02

Chris F


People also ask

What is {{ item }} Ansible?

item is not a command, but a variable automatically created and populated by Ansible in tasks which use loops. In the following example: - debug: msg: "{{ item }}" with_items: - first - second. the task will be run twice: first time with the variable item set to first , the second time with second .

Can we use loop in Ansible?

Ansible offers the loop , with_<lookup> , and until keywords to execute a task multiple times.

What is Loop VAR in Ansible?

Ansible loop is used to repeat any task or a part of code multiple times in an Ansible-playbook. It includes the creation of multiple users using the user module, installing multiple packages using apt or yum module or changing permissions on several files or folders using the file module.


1 Answers

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.

like image 135
tmoschou Avatar answered Oct 11 '22 10:10

tmoschou