Ansible: 'variable' is undefined
A variable used in a playbook or template was not defined in inventory, group_vars, host_vars, or the playbook itself.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
# Define in playbook vars
vars:
my_var: value
# Or in group_vars/all.yml
# Or provide at runtime
ansible-playbook playbook.yml -e "my_var=value"
# Use default filter
{{ my_var | default('fallback') }}
How to diagnose Ansible errors
Ansible errors split cleanly between transport problems (it cannot reach or authenticate to the host) and execution problems (it reached the host but the play failed). The message header tells you which: UNREACHABLE! is transport, FAILED! is execution. Transport issues are SSH issues wearing an Ansible hat and should be debugged with plain ssh first.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Run
ansible -m ping allbefore running any playbook. It isolates connectivity and privilege escalation from your task logic. - Raise verbosity progressively:
-vshows results,-vvvshows the SSH command line,-vvvvshows connection debugging. Most unreachable errors are solved at-vvv. - Use
--check --diffto see what a play would change without changing it. This turns a destructive debugging loop into a safe one. - For undefined-variable errors, dump the actual scope with a
debugtask (var: hostvars[inventory_hostname]) rather than reasoning about precedence from the docs. - Remember variable precedence order: extra vars (
-e) beat everything, role defaults lose to everything. Surprising values are nearly always a precedence surprise.
Tools worth reaching for
ansible -m pingansible-playbook --check --diffansible-lintssh -vvv
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related Ansible errors
- Ansible: Host unreachableCannot connect to target host via SSH. Network issues, wrong IP, SSH not running, or key…
- Ansible: Jinja2 template errorA Jinja2 template in a playbook or template file has a syntax error such as unclosed braces…
- Ansible: Module not foundSpecified module doesn't exist or isn't installed. Wrong module name or collection not…
- Ansible: Playbook syntax errorYAML syntax error in playbook. Indentation issues, invalid YAML, or missing required fields.
- Ansible: Privilege escalation password requiredTask requires sudo but password not provided. Target user needs sudo password for privilege…
- Ansible: SSH connection timeoutAnsible timed out trying to establish an SSH connection to the target host. The host may be…
- Ansible: The conditional check 'result.rc == 0' failedThe when expression could not be evaluated, usually because the variable it names does not…
- Ansible: Vault password not providedA playbook uses encrypted vault variables but no vault password was supplied at runtime.
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.