Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Variables · Ansible

Direct 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.

When to use this

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

First 30 seconds

1. Variables: defaults, group_vars, host_vars, play vars, extra vars (highest).

2. Use defaults for role inputs.

3. Avoid overriding with extra vars casually.

4. Clear names (role prefix).

5. Secrets in vault.

Operational verification

sources by precedence → effective value

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 · Variables owns Ansible variable scope/precedence. Secret storage and external source-of-truth systems remain separate branches.
Scope / prerequisiteUse it when configuring, checking or troubleshooting Variables 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.

3 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

variable declarations/sources → precedence/scope → host context → Jinja/task argument resolution → action → target result

Healthy evidence: source and precedence are understood, effective value/type is verified in the failing context and target behavior matches the corrected input.

Step 2 Failure modes and next checks Narrow the fault

Suspicious evidence: duplicate names across layers, extra-var override, wrong type, host-specific divergence or debugging secrets by printing them indiscriminately.

Step 3 Common mistakes Narrow the fault
  • Not knowing the variable precedence order.
  • Overusing extra vars (-e), which override everything.
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
  • 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.
Key checks
  • ansible-inventory --host <h> — effective vars
  • ansible <h> -m debug -a 'var=<var>' — value
  • Variable precedence doc — 22 levels
ReferenceWhat it is1 section · depth on demand

Core model

Debug variables by name, type, source, scope, host context and precedence; resolve the effective value at the point of use instead of searching only for text definitions.

ReferenceImportant terms & boundaries2 sections · depth on demand

What this shows and what it does not

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

In depth

Ansible variables customise playbooks per host, group or run. They come from many places, and a fixed precedence order decides which value wins — a common source of surprises.

Source (low → high precedence, simplified)
Role defaults
Inventory group_vars/all, then other groups, then host_vars
Play vars and vars_files
Role vars
set_fact / registered vars
Extra vars (-e) — always win
ansible -i inventory/ sw01 -m ansible.builtin.debug -a 'var=ntp_servers'

Keep each variable defined in as few places as possible and document where.

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 > Variables. 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 variablesvariable precedencehost varsgroup vars
Embedded reference data

Quick lookup tables

Variables Ansible boundary map

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

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
Confirmation

Confirm action

Continue?