Your playbook crashes mid-execution with a cryptic "undefined variable" error. The variable seems like it should existβyou defined it in your inventory, or it should come from a fact. Ansible's variable precedence and scope rules can be confusing, leading to variables not being available where you expect them.
Introduction
This article covers troubleshooting steps and solutions for Fix Ansible Variable Undefined Error. The error typically occurs in production environments and can cause service disruptions if not addressed promptly.
Symptoms
fatal: [webserver]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'app_port' is undefined.\n\nThe error appears to be in '/home/user/playbook.yml': line 15, column 7"
}Or in a template:
fatal: [webserver]: FAILED! => {
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'database_host'"
}Common Causes
- Configuration misconfiguration
- Missing or incorrect credentials
- Network connectivity issues
- Version compatibility problems
- Resource exhaustion or limits
- Permission or access denied
Step-by-Step Fix
Debug your variables to see what's actually defined:
```bash # Show all variables for a host ansible webserver -m debug -a "var=hostvars[inventory_hostname]"
# Show a specific variable ansible webserver -m debug -a "var=app_port"
# Show all variables in a playbook - name: Debug all variables debug: var: vars ```
Common Causes and Fixes
Variable Not Defined in Scope
Variables have scope. A variable defined in one play isn't automatically available in another.
Problem: ```yaml - hosts: webservers vars: app_port: 8080 tasks: - name: Configure app template: src: app.conf.j2 dest: /etc/app.conf # app_port is available here
- hosts: dbservers
- tasks:
- - name: Configure db
- template:
- src: db.conf.j2
- dest: /etc/db.conf
- # app_port is NOT available here!
`
Fix: Define in group_vars or host_vars:
mkdir -p group_varsgroup_vars/all.yml:
``yaml
app_port: 8080
Now the variable is available to all hosts in all plays.
Wrong Variable Name or Typo
A simple typo causes undefined errors.
Debug the actual variable name:
``yaml
- name: Show available variables
debug:
msg: "Available vars: {{ vars.keys() | list }}"
Use default to prevent crashes:
``yaml
- name: Use port with default
debug:
msg: "Port is {{ app_port | default(80) }}"
Dictionary Attribute Not Existing
Accessing a nested attribute that doesn't exist:
- name: Show database host
debug:
msg: "{{ config.database.host }}"If config or database is missing, you get an undefined error.
Fix with default:
``yaml
- name: Show database host with default
debug:
msg: "{{ config.database.host | default('localhost') }}"
Or use the safer bracket notation:
``yaml
- name: Show database host
debug:
msg: "{{ config['database']['host'] | default('localhost') }}"
Fact Variable Not Gathered
Ansible facts are only gathered if gather_facts: true (the default).
Problem:
``yaml
- hosts: all
gather_facts: false
tasks:
- name: Show IP
debug:
msg: "{{ ansible_default_ipv4.address }}"
This fails because facts weren't gathered.
Fix:
``yaml
- hosts: all
gather_facts: true # or remove this line (true is default)
tasks:
- name: Show IP
debug:
msg: "{{ ansible_default_ipv4.address }}"
Or gather facts manually: ```yaml - hosts: all gather_facts: false tasks: - name: Gather facts setup:
- name: Show IP
- debug:
- msg: "{{ ansible_default_ipv4.address }}"
`
Inventory Variable Not Found
Variables defined in inventory might not load if the inventory path is wrong.
Check inventory is loaded:
``bash
ansible-inventory --list
Verify host variables:
``bash
ansible-inventory --host webserver
Make sure ansible.cfg points to inventory:
``ini
[defaults]
inventory = ./inventory/hosts
Variable Precedence Confusion
Ansible has 22 levels of variable precedence. A variable might be overridden by one you didn't expect.
- 1.From lowest to highest precedence (simplified):
- 2.Command line values (
-e "var=value") - 3.Role defaults
- 4.Inventory file
- 5.Inventory group_vars
- 6.Inventory host_vars
- 7.Playbook vars
- 8.Host facts
- 9.Role vars
- 10.Block vars
- 11.Task vars
- 12.Extra vars (
-e)
Debug which value wins:
``yaml
- name: Show variable source
debug:
msg: "app_port={{ app_port }} from {{ lookup('pipe', 'echo $WHERE_IT_CAME_FROM') }}"
Using set_fact Incorrectly
Facts set in one task might not be available in the same play's pre_tasks or roles.
```yaml - hosts: all pre_tasks: - name: Set a fact set_fact: my_var: "value"
roles: - role: myrole # my_var is available here ```
But if you use vars instead of set_fact:
- hosts: all
vars:
my_var: "{{ some_other_var }}"
# This evaluates lazily and may fail if some_other_var isn't defined yetVerification
Always verify variables are defined before using them:
- name: Assert variable is defined
assert:
that:
- app_port is defined
- app_port | int > 0
fail_msg: "app_port must be defined and positive"
success_msg: "app_port is valid"Prevention
- 1.Use defaults:
{{ var | default('value') }} - 2.Define in group_vars: For shared variables
- 3.Assert at playbook start: Catch missing vars early
- 4.Document required variables: In role meta or comments
```yaml # roles/myrole/defaults/main.yml app_port: 8080 app_host: localhost
# roles/myrole/tasks/main.yml - name: Assert required variables assert: that: - app_name is defined fail_msg: "app_name is required" ```
Related Articles
- [WordPress troubleshooting: Ansible Artifact Download Uses an Old Mi](ansible-artifact-download-uses-an-old-mirror-after-proxy-change)
- [WordPress troubleshooting: Ansible Audit Trail Misses Events Under ](ansible-audit-trail-misses-events-under-burst-load)
- [WordPress troubleshooting: Ansible Background Worker Gets Stuck in ](ansible-background-worker-stuck-in-a-retry-loop)
- [WordPress troubleshooting: Ansible Backup Completes but Restore Fai](ansible-backup-completes-but-restore-fails-checksum-validation)
- [WordPress troubleshooting: Ansible Batch Importer Duplicates Rows A](ansible-batch-importer-duplicates-rows-after-a-retry)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Ansible Variable Undefined Error", "description": "Learn how to fix Ansible undefined variable errors with proper defaults, variable precedence, and debugging techniques.", "url": "https://www.fixwikihub.com/fix-ansible-variable-undefined", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-12T01:38:00.172Z", "dateModified": "2025-11-12T01:38:00.172Z" } </script>