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 ---------------------------------