Looping Over Dictionaries
Create users from key-value data.
Looping Over Dictionaries is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Lists Are Not Enough
Creating users needs more than a name: a group, a shell, a state. A flat list cannot carry that. You need a list of dictionaries. 🗂️
A List of Dictionaries
Each list entry becomes a small dictionary of key-value pairs. One loop iteration then has several named fields to work with.
loop:
- { name: alice, group: admin }
- { name: bob, group: dev }Access Keys with item.key
When item is a dictionary, read each field with item.fieldname. So item.name and item.group pull values from the current entry.
- name: Add users
ansible.builtin.user:
name: "{{ item.name }}"
group: "{{ item.group }}"
loop:
- { name: alice, group: admin }
- { name: bob, group: dev }Cleaner Multi-Line Form
Inline braces get cramped. Write each dictionary across multiple lines for readability when entries have many keys.
loop:
- name: alice
group: admin
shell: /bin/bash
- name: bob
group: dev
shell: /bin/zshPull the List from Vars
Define your dictionary list as a variable, then loop over it. Data lives in one place; the task stays short and reusable.
vars:
users:
- { name: alice, group: admin }
tasks:
- ansible.builtin.user:
name: "{{ item.name }}"
loop: "{{ users }}"Looping a Real Dictionary
To iterate an actual dict of key-value pairs, pipe it through the dict2items filter so each pair becomes a loopable item.
loop: "{{ ports | dict2items }}"item.key and item.value
After dict2items, each item exposes item.key and item.value, the original mapping's name and its data.
- ansible.builtin.debug:
msg: "{{ item.key }} = {{ item.value }}"
loop: "{{ settings | dict2items }}"Mixed-Optional Keys
Not every entry needs every key. Combine a loop with the default filter so missing fields fall back to a sensible value.
shell: "{{ item.shell | default('/bin/sh') }}"Dictionaries Keep Data Tidy
Bundling related fields per item keeps your data and logic separate. Editing a user means editing data, never the task itself.
Still Fully Idempotent
Each dictionary iteration is its own module call, so Ansible reports changed or ok per user, exactly as with simple lists.
Great for Bulk Config
Users, virtual hosts, firewall rules: anything with several attributes per entry fits a list of dictionaries perfectly.
Quick Check
You loop over a list of dictionaries. How do you read a field?
Recap: Looping Dicts
You looped over lists of dictionaries, read item.name fields, and used dict2items for real maps. Next up: nested loops. 🎉
Frequently asked questions
Is the “Looping Over Dictionaries” lesson free?
Yes — the full text of “Looping Over Dictionaries” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Looping Over Dictionaries”?
Create users from key-value data. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Looping Over Dictionaries” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The loop Keyword Over a List
- Looping Over Dictionaries
- Nested Loops with subelements
- loop_control: label, index & pause