Updated May 15, 2023
Introduction to Ansible inventory_hostname
Ansible inventory_hostname is one of special variables, this variable contains the name of a host as defined or configured in Ansible inventory file, which by default is /etc/ansible/hosts. Ansible provides few ways by which you can use the information related to your target machines, these ways include Ansible Facts, Magic Variables, and Connection Variables. Ansible inventory_hostname is among the highly used Magic Variables set, that also includes groups, hostvars, group_names. This variable is useful when you are relying more on your Ansible inventory file rather than actual information of remote hosts or you do not have many options to gather such information.
What is Ansible inventory_hostname?
Using Ansible Magic Variable inventory_hostname is useful in scenarios where a remote machine’s hostname is very long or have special characters which may create issues in playbook’s execution. Also when you don’t want to rely on ansible_hostname (contains actual hostname after gathering facts from system), which also means if you have set gather_facts to no, in your playbook, you need something to replace it. In all such cases, inventory_hostname has the real use.
There is another version of it named as inventory_hostname_short, which is used to shorten the long hostnames. Like if someone has a long FQDN, this Ansible Magic Variable is used to take the part of it up to first period (.) and leave rest of the parts containing domain names. This variable is providing you another option along with ansible_hostname and hostvars, to choose target machine hostname, which will be realized and used by Ansible playbook during execution.
How does Ansible inventory_hostname work?
Ansible Magic Variable inventory_hostname is a variable indeed, so it issued likewise in a playbook. You do not need to define it anywhere, as this is provided by Ansible and contains the host’s name defined in your inventory file. The inventory file can be a customized one, on customized path or the default one viz. /etc/ansible/hosts.
Now below are some important points to note when working with this variable:
- This variable can refer to an alias name if you have defined it in Ansible inventory file and set other related variables against it.
- If you are trying to work, based on a host’s entry in Ansible inventory file, then that entry must resolve to some IP by active DNS. Otherwise, Ansible cannot understand it and refuse to connect.
Examples of Ansible inventory_hostname
Now by using examples, we will try to see more about the Ansible inventor_hostname, which you might have to use in your day-to-day operations. We will take some examples, but before going there, we should first understand our lab, we used for testing purpose. In the lab, there is an ansible-controller server where we are running our commands and where inventory file resides. As target machines we have two machines with hostnames as host-one (RHEL based machines) and host-two-ubuntu (Ubuntu based machines). On these local and target machines, we will try to do some changes using Ansible.
Example #1
In this example, we will try to see the difference between the ansible_hostname and inventory_hostname variable. For this, we created a sample inventory file with the below contents. Also, we have enabled gather_facts.
Code:
---
- hosts: all
gather_facts: yes
tasks:
- name: Here we are checking ansible_hostname
debug:
msg: "hostname is {{ ansible_hostname }}"
- name: Here we are checking inventory_hostname
debug:
msg: "hostname is {{ inventory_hostname }}"
In our inventory custom inventory file, we have contents like below, mentioned only target machine’s name, but please note these are resolvable to IP by my DNS:
- server1
- server2
Now running this playbook like below:
Code:
ansible-playbook ansible_inventory_hostname.yaml -i custome_inventory.ini
In the output, you can see that, firstly, actual hostnames of target machines were displayed as we used ansible_hostname in our first task, then in second task we have used inventory_hostname, so the values mentioned in custom inventory files are displayed which are server1 and server2.
Output:
Example #2
In this example, we are going to create a file on remote hosts, using Jinja2 templating. In this file we are going to put values of inventory_hostname and ansible_hostname. For this, we are using the same inventory file used in previous example, but now we are updating it and now it looks like below now, as now i have removed entry of server2 from my DNS, so we can’t resolve it by DNS, thus we shall explicitly mention the IP using ansible_ssh_host like below.
Similarly you can mention per values in inventory file when those values, by default, are not available in the environment.
- server1
- server2 ansible_ssh_host=172.31.39.35
Also, we have created a jinja2 template with which we will put the values in a file on target machines.
This template has contents like below:
- For first host, ansible_hostname is {{ ansible_hostname }} and inventory_hostname is {{ inventory_hostname }}
- For second host, ansible_hostname is {{ ansible_hostname }} and inventory_hostname is {{ inventory_hostname }}
The playbook looks like below, in this playbook we are using Ansible template module to create file on remote hosts:
Code:
---
- hosts: all
gather_facts: yes
tasks:
- name: Here we are displaying the inventory_hostname and ansible_hostname values.
debug:
msg: "ansible_hostname = {{ ansible_hostname }} and inventory_hostname = {{ inventory_hostname }}"
- name: Here we are copying the Jinja2 template to remote machines
template:
src: host_names.j2
dest: /var/tmp/host_names
In the output, you see like below, which means files have been created on target hosts:
Code:
ansible-playbook ansible_inventory_hostname_create_file.yaml -i custome_inventory.ini
Output:
On checking the contents of these files on remote hosts:
Conclusion
As we saw and discussed in this example, if your playbook and environment have special need and restrictions, then you can make use of Ansible Magic Variables like inventory_hostname. That basically provides flexibility in Ansible environment and you have now more than one options to deal with the remote target machine’s data.
Recommended Articles
This is a guide to Ansible inventory_hostname. Here we discuss how does ansible inventory_hostname work with respective examples. You may also have a look at the following articles to learn more –