Ansible uses a declarative language using YAML that allows us to define the tasks that we wish to execute on the remote hosts. It does this using various modules and tools that are tailored to specific tasks.
However, it also allows us to define the variables that we can use to store the values that can be referenced in other parts of the playbook.
Ansible also has pre-defined variables that are known as magic variables that store the information about the system they manage and the Ansible controller itself.
In this post, we will explore the various magic variables in Ansible, what they do, and how we can reference them in our playbooks.
Ansible Magic Variables
In Ansible, variables help us parameterize our automation playbooks, making them more versatile.
Among the many variables that Ansible offers, a unique set is often called “magic” due to the automatic, behind-the-scenes information that they carry.
These magic variables can provide insights into various facets of the Ansible run such as details about the host, group, user, and more.
Unlike the standard variables, you, as the user, cannot set the values of these variables directly. This is because Ansible always overrides them to reflect the internal state of the target host or Ansible controller.
Ansible Top Magic Variables
The following are some commonly used magic variables in Ansible:
ansible_config_file – It specifies the full path to the Ansible configuration file.
ansible_inventory_sources – It specifies a list of sources that is used as inventory.
ansible_play_hosts – It specifies the list of hosts in the current play run that is not limited by the serial.
Ansible_role_name – It specifies the full qualified collection role name in the “namespace.collection.role_name” format.
Ansible_collection_name – It sets the name of the collection that the task executes where it is a part of.
Ansible_verbosity – It sets the current verbosity level for Ansible.
Ansible_version – It is a dictionary or map that contains the information about the current Ansible version.
Inventory_hostname – It is the inventory name for the current host that is being iterated over in the play.
Role_name – It is the name of the role that is currently executing.
Ansible_facts – It contains the facts that are gathered or cached for the inventory_hostname.
Ansible_become_user – It is the user that Ansible becomes after privilege escalation.
Ansible_host – It is the ip/hostname of the target host to use instead of the inventory_hostname.
Ansible_python_interpreter – It is the path to the Python executable that Ansible should use on the target host.
Examples of Ansible Magic Variables in Action
Let us look at some examples to understand better how to use these magic variables.
Accessing Facts
To get the facts that are gathered about a given host. We can use the ansible_facts as shown in the following playbook:
- hosts: all
tasks:
- name: Display facts
debug:
msg: "facts: {{ansible_facts }}"
This should return the facts that are gathered about the target hosts.
Conclusion
Ansible’s magic variables are powerful tools that can significantly enhance the versatility and adaptability of your playbooks. By tapping into their meta information, you can build the automation scripts that are aware of their environment which makes them more dynamic and responsive.