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
How to Use Ansible Magic Variables in Ansible Playbook
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_file
- ansible_check_mode
- ansible_diff_mode
- ansible_forks
- ansible_verbosity
- inventory_