Ansible Pilot

Ansible 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

May 30, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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:

The path to the directory of the playbook that was passed to the ansible-playbook command line

The directory of the inventory source in which the inventory_hostname was first defined

The file name of the inventory source in which the inventory_hostname was first defined

The inventory name for the ‘current’ host is being iterated over in the play

Boolean that indicates if we are in check/diff mode or not

Dictionary/map that contains information about the currently running version of ansible, it has the following keys: full, major, minor, revision and string.

The full list is available on the official Ansible website Magic variables

The Best Resources For Ansible

Video Course

Books

demo

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:

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.

The directory of the inventory source in which the inventory_hostname was first defined

The file name of the inventory source in which the inventory_hostname was first defined

Boolean that indicates if we are in check mode or not

Boolean that indicates if we are in diff mode or not

Integer reflecting the number of maximum forks available to this run

Current verbosity setting for Ansible

The inventory name for the ‘current’ host is being iterated over in the play

List of hosts in the current play run, not limited by the serial. Failed/Unreachable hosts are excluded from this list.

Dictionary/map that contains information about the currently running version of ansible, it has the following keys: full, major, minor, revision and string.

code

---
- name: magic vars demo
  hosts: all
  gather_facts: false
  tasks:
    - name: magic variable
      ansible.builtin.debug:
        var: "{{ item }}"
      loop:
        - ansible_config_file
        - playbook_dir
        - inventory_dir
        - inventory_file
        - ansible_check_mode
        - ansible_diff_mode
        - ansible_forks
        - ansible_verbosity
        - inventory_hostname
        - ansible_play_hosts
        - ansible_version

execution

$ ansible-playbook -i virtualmachines/demo/inventory variables/magic.yml
PLAY [magic vars demo] ****************************************************************************
TASK [magic variable] *****************************************************************************
ok: [demo.example.com] => (item=ansible_config_file) => {
    "ansible_config_file": null,
    "ansible_loop_var": "item",
    "item": "ansible_config_file"
}
ok: [demo.example.com] => (item=playbook_dir) => {
    "ansible_loop_var": "item",
    "item": "playbook_dir",
    "playbook_dir": "/Users/lberton/prj/github/ansible-pilot/variables"
}
ok: [demo.example.com] => (item=inventory_dir) => {
    "ansible_loop_var": "item",
    "inventory_dir": "/Users/lberton/prj/github/ansible-pilot/virtualmachines/demo",
    "item": "inventory_dir"
}
ok: [demo.example.com] => (item=inventory_file) => {
    "ansible_loop_var": "item",
    "inventory_file": "/Users/lberton/prj/github/ansible-pilot/virtualmachines/demo/inventory",
    "item": "inventory_file"
}
ok: [demo.example.com] => (item=ansible_check_mode) => {
    "ansible_check_mode": false,
    "ansible_loop_var": "item",
    "item": "ansible_check_mode"
}
ok: [demo.example.com] => (item=ansible_diff_mode) => {
    "ansible_diff_mode": false,
    "ansible_loop_var": "item",
    "item": "ansible_diff_mode"
}
ok: [demo.example.com] => (item=ansible_forks) => {
    "ansible_forks": 5,
    "ansible_loop_var": "item",
    "item": "ansible_forks"
}
ok: [demo.example.com] => (item=ansible_verbosity) => {
    "ansible_loop_var": "item",
    "ansible_verbosity": 0,
    "item": "ansible_verbosity"
}
ok: [demo.example.com] => (item=inventory_hostname) => {
    "ansible_loop_var": "item",
    "inventory_hostname": "demo.example.com",
    "item": "inventory_hostname"
}
ok: [demo.example.com] => (item=ansible_play_hosts) => {
    "ansible_loop_var": "item",
    "ansible_play_hosts": [
        "demo.example.com"
    ],
    "item": "ansible_play_hosts"
}
ok: [demo.example.com] => (item=ansible_version) => {
    "ansible_loop_var": "item",
    "ansible_version": {
        "full": "2.12.5",
        "major": 2,
        "minor": 12,
        "revision": 5,
        "string": "2.12.5"
    },
    "item": "ansible_version"
}
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Recap

Now you know how to use the Ansible Magic Variables in Ansible Playbook. You know how to use it based on your use case.

Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 200+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress