From b016cdd7ff044f1aa8d6c6e371702577a8c1c788 Mon Sep 17 00:00:00 2001 From: GABRIEL OVIE Date: Mon, 20 Oct 2025 14:45:07 +0100 Subject: [PATCH] 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) (cherry picked from commit 61ce435c0373bfdf21b96f66e89f4b618d698694) --- .../rst/playbook_guide/playbooks_loops.rst | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/docsite/rst/playbook_guide/playbooks_loops.rst b/docs/docsite/rst/playbook_guide/playbooks_loops.rst index d61e857cfa2..702dc60db71 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_loops.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_loops.rst @@ -144,14 +144,23 @@ To loop over a dict, use the :ref:`dict2items `: - 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.``. Registering variables with a loop ---------------------------------