AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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 'VARIABLE IS NOT DEFINED' Error: Undefined Variables — Video Tutorial
Fix Ansible VARIABLE IS NOT DEFINED error. Troubleshoot undefined variables, missing facts, gather_facts issues, and use default filter for safe access.
What You'll Learn
- Introduction
- Playbook
- error code
- error execution
- fix code
- fix execution
- Conclusion
- Why Does This Error Happen?
- Common Variables That Require gather_facts
- Variables That DON'T Require gather_facts
Full Tutorial Content
Introduction
Today we’re going to talk about Ansible troubleshooting, specifically about VARIABLE IS NOT DEFINED! Message.
Most of the time the root cause is a misspelled variable or a variable really not defined. This use case is special about the `ansible_hostname` internal variable.
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 VARIABLE IS NOT DEFINED! and how to solve it!
error code
```yaml
---
- name: hostname Playbook
hosts: all
gather_facts: false
tasks:
- name: print hostname
ansible.builtin.debug:
var: ansible_hostname
```
error execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/variablenotdefined_error.yml
PLAY [hostname Playbook] ******************************************************************************
TASK [print hostname] *****************************************************************************
ok: [demo.example.com] => {
"ansible_hostname": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
fix code
```yaml
---
- name: hostname Playbook
hosts: all
gather_facts: true
tasks:
- name: print hostname
ansible.builtin.debug:
var: ansible_hostname
```
fix execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory troubleshooting/variablenotdefined_fix.yml
PLAY [hostname Playbook] ******************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [print hostname] *****************************************************************************
ok: [demo.example.com] => {
"ansible_hostname": "Playbook"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting)
Conclusion
Now you know better how to troubleshoot the Ansible VARIABLE IS NOT DEFINED! message.
Why Does This Error Happen?
Ansible has two types of variables:
1. **User-defined variables** — you create them with `vars:`, `set_fact`, `register`, etc.
2. **Facts (built-in variables)** — Ansible collects them automatically from managed hosts
Facts like `ansible_hostname`, `ansible_os_family`, `ansible_distribution`, and `ansible_default_ipv4` are **only available if `gather_facts: true`** (which is the default). If you set `gather_facts: false`, these variables don't exist.
Common Vari
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 9 min
- Category: troubleshooting
Read the full written article: Ansible Fix 'VARIABLE IS NOT DEFINED' Error: Undefined Variables
Related Video Tutorials
- Ansible Fix Undefined Variable Error: Complete Troubleshooting Guide — Fix Ansible undefined variable errors. Use default filter, debug variable scope, handle missing facts, and prevent undefined variable issues in playbooks.
- Ansible 'Failed to Connect via SSH localhost:22': Fix Guide — Fix Ansible 'failed to connect to the host via ssh localhost port 22' error. Resolve SSH config, connection type, host key, and authentication issues.
- Ansible Vault Error: 'Attempting to decrypt but no vault secrets found' Fix — Fix Ansible error 'Attempting to decrypt but no vault secrets found'. Provide vault password via --ask-vault-pass, password file, or environment variable.
- Ansible troubleshooting - Destination does not exist rc 257 — Troubleshoot the "Destination does not exist" error (return code 257) in Ansible with Luca Berton on Ansible Pilot! Learn to fix file path issues effectively.
- Ansible troubleshooting - Module Failure on Windows-target — Discover how to troubleshoot Module Failure on Windows-target in Ansible, focusing on resolving execution errors effectively.
- Ansible Permission Denied (Errno 13): Fix File Access Errors — Fix Ansible Permission denied [Errno 13] errors. Resolve file permission, become/sudo, SELinux, and directory access issues with troubleshooting steps.