Loops
Cheat sheet · https://pcornel.dk/topic.php?topic=5409
Answer
Ansible loops execute a task repeatedly over items, allowing each iteration to have distinct inputs and result state while sharing one task definition.
Remember
- 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.
First checks
- loop: with loop_control.label — iteration
ansible-playbook site.yml --check— test loops- Prefer module list arguments (package name lists) — efficiency
Verify in this order
list → loop → task per item
Where it usually breaks
A loop task result summarizes repeated executions; it does not prove every item has identical inputs, result semantics or final resource state.
Inventory/vars → play/task selection → connection/become → module request → module result → target object read-back → handler/service effective state → workload verification.
Loops Ansible boundary map
| Boundary | Healthy evidence | Suspicious evidence |
|---|---|---|
| Inputs / selection | Correct inventory, hosts, variables/facts and content are explicit | Wrong scope, stale facts, hidden precedence or content-version drift |
| Execution | Connection, privilege, module/task context and ordering match intent | Unreachable, wrong execution host, privilege/plugin failure or misleading skip/change state |
| Result semantics | Per-host/item results are preserved and interpreted correctly | Aggregate recap hides partial/skipped/handled failures |
| Target proof | Resource/workload read-back and rerun confirm convergence | Green Ansible status treated as end-to-end acceptance |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5409