mirror of
https://github.com/ansible/ansible-documentation.git
synced 2026-03-27 13:28:51 +07:00
docs: clarify dict2items usage in loops with group module (follow-up to ansible#85897) (#3124) (#3156)
* 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 61ce435c03)
Co-authored-by: GABRIEL OVIE <ovieorezi@gmail.com>
This commit is contained in:
@@ -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
|
||||
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 `tag_data` and printing the key and the value from it.
|
||||
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
|
||||
---------------------------------
|
||||
|
||||
Reference in New Issue
Block a user