Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Loops · Ansible

Direct answer

Ansible loops execute a task repeatedly over items, allowing each iteration to have distinct inputs and result state while sharing one task definition.

When to use this

Use it when configuring, checking or troubleshooting Loops in Ansible.

First 30 seconds

1. loop iterates over lists/dicts.

2. loop_control label for readable output.

3. Many modules accept lists directly (faster).

4. Avoid nested loops when possible.

5. Register results per item.

Operational verification

list → loop → task per item

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Ansible automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformAnsible automation · Loops owns Ansible item iteration semantics. Module behavior and target resource state remain adjacent boundaries.
Scope / prerequisiteUse it when configuring, checking or troubleshooting Loops in Ansible.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

4 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Operational evidence path Verify boundary

loop expression/items → per-item variable resolution → repeated module action → per-item result list → aggregate task result → resource read-back

Healthy evidence: item set is intentional, per-item results are reviewed, failures localize to specific inputs and target resources match expected iteration outcomes.

Step 2 Failure modes and next checks Narrow the fault

Suspicious evidence: unexpected item expansion, hidden failed item, wrong loop variable, mixed result states or reading only aggregate task color/status.

Step 3 Common mistakes Narrow the fault
  • with_items and loop differences with nested lists.
  • Looping one task per item when the module accepts a list.
Step 4 Where it fails Narrow the fault

Where Loops usually goes wrong: iteration identity, per-item failure/partial success, ordering and bounded retry behavior.

Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • Task succeeded overall ≠ every item produced the intended target state.
  • Loop item label ≠ full item data; labels can intentionally hide or shorten values.
  • changed=true ≠ every iteration changed; results are per item.
  • Retry/loop interaction ≠ single global retry; behavior applies at task/item boundaries depending on construct.
Key checks
  • loop: with loop_control.label — iteration
  • ansible-playbook site.yml --check — test loops
  • Prefer module list arguments (package name lists) — efficiency
ReferenceWhat it is1 section · depth on demand

Core model

Treat loop expansion, item identity, per-item variables, result list and failure/change aggregation separately; one task summary can hide item-specific outcomes.

ReferenceImportant terms & boundaries2 sections · depth on demand

What this shows and what it does not

A loop task result summarizes repeated executions, but it does not show every item has identical inputs, result semantics or final resource state.

In depth

Ansible loops repeat a task over a list or dictionary with loop:, exposing each item as item (renameable with loop_control).

- name: Create VLANs
  cisco.ios.ios_vlans:
    config:
      - vlan_id: "{{ item.id }}"
        name: "{{ item.name }}"
    state: merged
  loop: "{{ vlans }}"
  loop_control:
    label: "{{ item.id }}"

Many modules accept a full list in one call (resource modules, package with a list of names); passing the list once is faster and more atomic than looping.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docAnsible automation current

Authoritative/upstream documentation reviewed for bounded claims in Ansible > Loops. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://docs.ansible.com/ansible/latest/ · confidence 94%
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
ansible loopwith_itemsloop_controlregistered loop results
Embedded reference data

Quick lookup tables

Loops Ansible boundary map

Fast input/selection, execution/result and target-proof boundaries for Loops.

BoundaryHealthy evidenceSuspicious evidence
Inputs / selectionCorrect inventory, hosts, variables/facts and content are explicitWrong scope, stale facts, hidden precedence or content-version drift
ExecutionConnection, privilege, module/task context and ordering match intentUnreachable, wrong execution host, privilege/plugin failure or misleading skip/change state
Result semanticsPer-host/item results are preserved and interpreted correctlyAggregate recap hides partial/skipped/handled failures
Target proofResource/workload read-back and rerun confirm convergenceGreen Ansible status treated as end-to-end acceptance
Depth when you need it

Deep reference

Healthy vs suspicious evidence

Output interpretation

For Loops, rely on structured fields and stable IDs (iteration identity, per-item failure/partial success, ordering and bounded retry behavior) rather than text formatted for display.

Confirmation

Confirm action

Continue?