What are loops in Ansible and how do you iterate over items?
Understand Ansible loops and the item variable to iterate over lists and dictionaries, using loop and loop_control with clear, idiomatic examples.
Expected Interview Answer
Loops in Ansible let a single task run repeatedly over a set of values using the `loop` keyword, with each element exposed as the special variable `item`.
Instead of copying a task many times, you attach `loop:` with a list and reference `{{ item }}` inside the task. The list can be inline, a variable, or built with lookups; loops work over strings, dictionaries, and lists of dicts where you access fields like `item.name`. Modern Ansible prefers `loop` over the older `with_*` constructs, and you can customize iteration with `loop_control` to set a friendly `label`, an `index_var`, or a `pause` between iterations.
- Eliminates repetitive near-identical tasks
- Iterates over lists, dicts, and lists of dictionaries
- Keeps playbooks short and easy to maintain
- Works with registered results for per-item output
- loop_control gives cleaner output and index tracking
AI Mentor Explanation
A bowler delivers six balls in an over, repeating the same run-up and action for each while only the ball number changes. One routine, applied item by item across the over. An Ansible loop does exactly this — one task definition runs once per element in the list, with `item` standing for the current ball so the same action covers every delivery.
Step-by-Step Explanation
Step 1
Add loop to a task
Attach `loop:` with a list value — inline, a variable, or a lookup result.
Step 2
Reference item
Use `{{ item }}` inside the task to access the current element on each iteration.
Step 3
Loop over dictionaries
For a list of dicts, access fields like item.name and item.state within the module args.
Step 4
Tidy output with loop_control
Set loop_control.label to show a friendly per-iteration label instead of the whole item.
Step 5
Track the index
Use loop_control.index_var to capture the current iteration number when you need it.
What Interviewer Expects
- Uses loop with the item variable correctly
- Knows loop is preferred over legacy with_items
- Can iterate over a list of dictionaries and access fields
- Understands loop_control label and index_var
- Explains how registered results capture per-item output
Common Mistakes
- Still defaulting to with_items instead of loop in new code
- Forgetting {{ }} around item inside the task
- Accessing item.name when the list contains plain strings
- Not using loop_control.label, producing noisy output for large dicts
- Assuming loops run in parallel — they run sequentially per host
Best Answer (HR Friendly)
“Loops let one instruction repeat over many values instead of writing it out again and again. For example, a single task can create ten user accounts by looping over a list of names, which keeps the playbook short and far easier to maintain.”
Code Example
- name: Install several packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- git
- curl
- htop
- name: Create users from a list of dicts
ansible.builtin.user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
state: present
loop:
- { name: alice, groups: admins }
- { name: bob, groups: developers }
loop_control:
label: "{{ item.name }}"Follow-up Questions
- What is the difference between loop and with_items?
- How do you iterate over a dictionary's key/value pairs?
- What does loop_control.label do and why use it?
- How do you capture the output of each iteration in a registered variable?
- Can you nest loops, and what are the alternatives if you need to?
MCQ Practice
1. Which variable holds the current element inside an Ansible loop?
Ansible exposes the current iteration element as the special variable `item`.
2. Which is the modern, preferred keyword for iteration in Ansible?
`loop` is the recommended keyword; the older with_* forms are retained mainly for backwards compatibility.
3. To show a clean per-iteration label instead of the full item, you use:
loop_control.label sets a concise label for each iteration in the task output.
Flash Cards
How do you loop a task in Ansible? — Attach loop: with a list and reference {{ item }} inside the task for each element.
loop vs with_items? — loop is the modern preferred syntax; with_items is a legacy alias kept for compatibility.
How do you loop over a list of dictionaries? — Use loop with the dict list and access fields like item.name and item.groups.
What does loop_control.label do? — Sets a short, readable label per iteration so output isn't cluttered with the full item.