Ansible Internal Magic Variables — Ansible Tip and Tricks

How to Ansible Internal Variables “Magic” could simplify our automation journey: running Ansible version, inventory details, and execution options.

Ansible Pilot
5 min readMay 30, 2022

--

How to Use Ansible Magic Variables in Ansible Playbook

I’m going to show you a live demo 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 https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html

demo

How to use Ansible Magic Variables in Ansible Playbook?

--

--

Ansible Pilot

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday