Pcornel
Automation · Ansible

Error Handling

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

Back to the Topic

Answer

Ansible error handling controls how failed, unreachable, changed or rescued tasks affect host progression using result conditions, blocks/rescue/always, retries and failure policy.

Remember

  • ignore_errors ≠ error resolved; it only changes control flow after a failure.
  • Rescue executed ≠ original desired state restored; compensation needs independent verification.
  • failed_when false ≠ command succeeded; policy can reclassify a nonzero/custom result.
  • Unreachable ≠ normal task failure; connection failures have distinct handling/state.

First checks

  • block/rescue/always — error handling
  • ignore_errors only deliberately — risk
  • failed_when conditions — correct failure detection

Verify in this order

task error → rescue → cleanup/rollback → report

Where it usually breaks

Handled/ignored/rescued status proves Ansible control flow continued according to policy; it does not prove root cause fixed or target state restored.

Error Handling 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=5418

Confirmation

Confirm action

Continue?