Skip to content

Commit 61ce435

Browse files
authored
docs: clarify dict2items usage in loops with group module (follow-up to ansible#85897) (#3124)
* docs: clarify dict2items usage in loops with group module (follow-up to ansible#85897) * docs: merge dict2items group example into existing section (per review) * docs: replace tag_data example with nested server_configs dict example (per review)
1 parent 6edfcb4 commit 61ce435

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

docs/docsite/rst/playbook_guide/playbooks_loops.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,23 @@ To loop over a dict, use the :ref:`dict2items <dict_filter>`:
144144

145145
- name: Using dict2items
146146
ansible.builtin.debug:
147-
msg: "{{ item.key }} - {{ item.value }}"
148-
loop: "{{ tag_data | dict2items }}"
147+
msg: "{{ item.key }}: {{ item.value.ip_address }} {{ item.value.role }}"
148+
loop: "{{ server_configs | dict2items }}"
149149
vars:
150-
tag_data:
151-
Environment: dev
152-
Application: payment
153-
154-
Here, we are iterating over `tag_data` and printing the key and the value from it.
150+
server_configs:
151+
web_01:
152+
ip_address: "10.1.1.50"
153+
role: "frontend"
154+
db_01:
155+
ip_address: "10.1.1.100"
156+
role: "backend_db"
157+
158+
Here, we are iterating over `server_configs` and printing the key and selected nested fields.
159+
160+
If the values in the dictionary are themselves dictionaries (for example, each group maps
161+
to a dict containing a ``gid``), remember that after applying ``dict2items`` each loop item
162+
has two attributes: ``item.key`` and ``item.value``. Access nested fields via
163+
``item.value.<field>``.
155164

156165
Registering variables with a loop
157166
---------------------------------

0 commit comments

Comments
 (0)