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.

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

Join 50+ hours of courses in our exclusive community

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

---
- 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_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 Playbook] ****************************************************************************
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/Playbook",
    "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

Conclusion

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, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

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 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza