Pcornel
Automation · Ansible

Loops

Cheat sheet · https://pcornel.dk/topic.php?topic=5409

Back to the Topic

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

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

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5409

Confirmation

Confirm action

Continue?