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?
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 .
Ansible offers the loop , with_<lookup> , and until keywords to execute a task multiple times.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With