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.

Watch Video

Watch "Ansible Magic Variables: Complete Reference with Examples" on YouTube

What You'll Learn

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

Read the full written article: Ansible Magic Variables: Complete Reference with Examples

Topics Covered

Related Video Tutorials