AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Fix Undefined Variable Error: Complete Troubleshooting Guide — Video Tutorial
Fix Ansible undefined variable errors. Use default filter, debug variable scope, handle missing facts, and prevent undefined variable issues in playbooks.
What You'll Learn
- Introduction
- Playbook
- error code
- error execution
- fix code
- fix execution
- Conclusion
- Why Variables Become Undefined
- Fix Strategies
- Strategy 1: Define the variable
Full Tutorial Content
Introduction
Today we're going to talk about Ansible troubleshooting, specifically about the undefined variable errors.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Playbook
The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the undefined variable error and how to solve it!
error code
- underfinedvariable_error.yml
```yaml
---
- name: debug module Playbook
hosts: all
tasks:
- name: debug message
ansible.builtin.debug:
msg: "{{ fruit }}"
```
error execution
```bash
$ ansible-playbook troubleshooting/undefinedvariable_error.yml
PLAY [file module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [debug message] ******************************************************************************
fatal: [demo.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'fruit' is undefined\n\nThe error appears to be in '/Users/lberton/prj/github/ansible-pilot/troubleshooting/undefinedvariable_error.yml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: debug message\n ^ here\n"}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
```
fix code
- underfinedvariable_fix.yml
```yaml
---
- name: debug module Playbook
hosts: all
vars:
fruit: "apple"
tasks:
- name: debug message
ansible.builtin.debug:
msg: "{{ fruit }}"
```
fix execution
```bash
$ ansible-playbook troubleshooting/undefinedvariable_fix.yml
PLAY [debug module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [debug message] ******************************************************************************
ok: [demo.example.com] => {
"msg": "apple"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting)
Conclusion
Now you know how to troubleshoot the most common Ansible undefined variable error.
Why Variables Become Undefined
1. **Typo in variable name** — most common cause
2. **Variable defined in wrong scope** — defined in one play/role but used in another
3. **Missing `vars:` section** — forgot to define the variable
4. **Conditional variable** — variable only exi
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 9 min
- Category: installation
Read the full written article: Ansible Fix Undefined Variable Error: Complete Troubleshooting Guide