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 Magic Variables: Complete Reference with Examples — Video Tutorial
Complete reference for Ansible magic variables. Use inventory_hostname, hostvars, groups, play_hosts, ansible_facts, and special variables in playbooks.
What You'll Learn
- How to Use Ansible Magic Variables in Ansible Playbook
- Ansible Magic Variables
- Links
- code
- execution
- Conclusion
- Key Magic Variables
- hostvars (Access Other Host Data)
- groups (All Inventory Groups)
- inventory_hostname vs ansible_hostname
Full Tutorial Content
How to Use Ansible Magic Variables in Ansible Playbook
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Magic Variables
How to Ansible Magic Variables in Ansible Playbook.
The good news is that Ansible provides some internal variables that come out of the box with some information such as running the Ansible version, inventory details, or execution options.
Some examples:
- `playbook_dir`
The path to the directory of the playbook that was passed to the ansible-playbook command line
- `inventory_dir`
The directory of the inventory source in which the inventory_hostname was first defined
- `inventory_file`
The file name of the inventory source in which the inventory_hostname was first defined
- `inventory_hostname`
The inventory name for the 'current' host is being iterated over in the play
- `ansible_check_mode` / `ansible_diff_mode`
Boolean that indicates if we are in check/diff mode or not
- `ansible_version`
Dictionary/map that contains information about the currently running version of ansible, it has the following keys: full, major, minor, revision and string.
Links
The full list is available on the official Ansible website [Magic variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)
## Playbook
How to use Ansible Magic Variables in Ansible Playbook?
Let's see in action some of the most common Ansible Magic Variables in an Ansible Playbook.
I'm going to display the current value of the following variables:
- `ansible_config_file`
The full path of the used Ansible configuration file
playbook_dir
The path to the directory of the playbook was passed to the ansible-playbook command line.
- `inventory_dir`
The directory of the inventory source in which the inventory_hostname was first defined
- `inventory_file`
The file name of the inventory source in which the inventory_hostname was first defined
- `ansible_check_mode`
Boolean that indicates if we are in check mode or not
- `ansible_diff_mode`
Boolean that indicates if we are in diff mode or not
- `ansible_forks`
Integer reflecting the number of maximum forks available to this run
- `ansible_verbosity`
Current verbosity setting for Ansible
- `inventory_hostname`
The inventory name for the 'current' host is being iterated over in the play
- `ansible_play_hosts`
List of hosts in the current play run, not limited by the serial. Failed/Unreachable hosts are excluded from this list.
- `ansible_version`
Dictionary/map that contains information about the currently running version of ansible, it has the following keys: full, major, minor, revision and string.
code
```yaml
---
- name: magic vars Playbook
hosts: all
gather_facts: false
tasks:
- name: magic variable
ansible.builtin.debug:
var: "{{ item }}"
loop:
- ansible_config_file
- playbook_dir
- inventory_dir
- inventory_f
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 9 min
- Category: troubleshooting
Read the full written article: Ansible Magic Variables: Complete Reference with Examples
Related Video Tutorials
- Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide) — How to use Ansible's playbook_dir magic variable to get the current playbook directory path. Includes role_path, inventory_dir, and all magic variables.
- ansible.cfg Configuration File: Complete Settings Guide (2026) — Complete guide to ansible.cfg configuration file. Every section explained — defaults, privilege_escalation, ssh_connection, inventory, galaxy.
- Ansible inventory_hostname vs ansible_hostname vs ansible_fqdn (Explained) — Difference between Ansible inventory_hostname, ansible_hostname, ansible_host, and ansible_fqdn variables.
- Ansible Disable SSH Host Key Checking: Configuration Guide — How to disable SSH host key checking in Ansible. Configure ansible.cfg, environment variables, and per-host settings for lab and production environments.
- Ansible Fix 'VARIABLE IS NOT DEFINED' Error: Undefined Variables — Fix Ansible VARIABLE IS NOT DEFINED error. Troubleshoot undefined variables, missing facts, gather_facts issues, and use default filter for safe access.
- 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.