Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions docs/docsite/rst/playbook_guide/playbooks_loops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,23 @@ To loop over a dict, use the :ref:`dict2items <dict_filter>`:

- name: Using dict2items
ansible.builtin.debug:
msg: "{{ item.key }} - {{ item.value }}"
loop: "{{ tag_data | dict2items }}"
msg: "{{ item.key }}: {{ item.value.ip_address }} {{ item.value.role }}"
loop: "{{ server_configs | dict2items }}"
vars:
tag_data:
Environment: dev
Application: payment

Here, we are iterating over `tag_data` and printing the key and the value from it.
server_configs:
web_01:
ip_address: "10.1.1.50"
role: "frontend"
db_01:
ip_address: "10.1.1.100"
role: "backend_db"

Here, we are iterating over `server_configs` and printing the key and selected nested fields.

If the values in the dictionary are themselves dictionaries (for example, each group maps
to a dict containing a ``gid``), remember that after applying ``dict2items`` each loop item
has two attributes: ``item.key`` and ``item.value``. Access nested fields via
``item.value.<field>``.

Registering variables with a loop
---------------------------------
Expand Down