Pcornel
Automation · Ansible

Variables

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

Back to the Topic

Answer

Ansible variables provide runtime data to plays, roles, tasks and templates, with scopes and a precedence model that can make the effective value differ from the nearest visible declaration.

Remember

  • Variable defined ≠ variable value used; higher-precedence sources can override it.
  • Same name across hosts ≠ same value; host/group/fact/context can differ.
  • String that looks boolean/number ≠ same data type; YAML/Jinja typing can change comparisons.
  • Secret variable hidden in output ≠ securely sourced or least-privileged.

First checks

  • ansible-inventory --host <h> — effective vars
  • ansible <h> -m debug -a 'var=<var>' — value
  • Variable precedence doc — 22 levels

Verify in this order

sources by precedence → effective value

Where it usually breaks

Finding a variable declaration proves only one candidate source; the effective runtime value is proven at the consumption context after precedence and templating.

Inventory/vars → play/task selection → connection/become → module request → module result → target object read-back → handler/service effective state → workload verification. Preserve at least three independent checks: prerequisite/interface or identity/transport state; the owning control/service/policy state; then effective data-plane, backend or workload state. A healthy upstream boundary does not prove the downstream consumer or return path.

Variables 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=5403

Confirmation

Confirm action

Continue?