Ansible Inventory_hostname & ansible_hostname Examples

In this post, we are going to see two built-in variables of ansible mostly used in Ansible playbooks and they are inventory_hostname and ansible_hostname while both these variables are to give you the hostname of the machine. they differ in a way, where it comes from.

So in this article, we are going to cover both of these variables in detail and compare the differences between them.

ansible_hostname

Inventory_hostname variable - Introduction

ansible's inventory_hostname is a built-in variable. It takes the hostname of the machine from the inventory script or the ansible configuration file.

Since the value is taken from the configuration file we are defining,  the actual runtime hostname value might vary from what is returned by this variable.

Mostly, In local development environments with Vagrant, we would not have a fully qualified hostname or DNS names for our virtual machines. so we often make entries in /etc/hosts file on the control machine to refer our hosts under the host group

Or we sometimes use the public/private IP address beneath our host groups especially while using ansible with AWS ec2 instances

something like this

[ec2servers]
53.45.21.70
58.43.89.21

[vagrantservers]
mwiapp01.example.com
mwiapp02.example.com

the inventory_hostname's responsibility is to refer to these hostnames/IPs mentioned in the default ansible_hosts file or any hostfile you are referring with ansible with -i flag when you are starting the playbook

ansible-playbook -i /tmp/customized_ansible_hosts

sometime it might return an IP if it has been defined that way in the ansible hosts file. refer the ec2servers hostgroup mentioned earlier.

ansible_hostname variable - Introduction

ansible_hostname built-in variable holds the hostname of the remote host just like the inventory_hostname, the difference is that ansible_hostname takes the hostname of the remote machine from the facts collected during the gather_facts section of your playbook.

Refer this article to know more about ansible facts and how they are collected

ansible_hostname takes the uname -n or the hostname command output of the remote machine.  this can be different from what you have used to connect or what is defined in the ansible hosts file.

Let's suppose you want to start a service in the remote server and try to access it from the control machine using the hostname of the remote server taken using the ansible_hostname.

This may not work if the hostname we defined in the hosts file and the actual hostname of the remote machine is different.

The best example is the AWS EC2 instances. you might have noticed that EC2 servers would have the default hostname something like  ip-172-89.29.12 this is a combination of keyword ip and the private ip of the instance but this may not be the hostname or the IP address we might define in the ansible hosts file

 

Ansible_hostname vs inventory_hostname in a nutshell

 

ansible_hostname  inventory_hostname
Ansible_hostname takes the hostname from the facts collected during the gather_facts this would mostly match to the uname -n or hostname command that you run on the remote machine inventory_hostname takes the hostname from the inventory configuration or the hosts file. this may not match the hostname configuration of the remote system. this could just be a local identity mentioned on the control machine
If the Gather_facts is set to NO the ansible_facts variable would not be available to use in your playbook inventory_hostname would always be available to use in your playbook.
Since the data taken from the configuration file. this may not match the uname -n or hostname command output of the remote machine  Since this is taken from the facts collected at runtime this would have the same hostname defined in system configuration like /etc/hostname and match the output of uname -n and hostname
As this is based on the Gather_facts step. ansible_hostname not available in ad_hoc command  Available for both playbook and ad hoc command

 

Ansible Playbook example with ansible_hostname and inventory_hostname

This ansible playbook has been created to demonstrate the major differences between the inventory_hostname and the ansible_hostname variables and how it works in real time

 

A Simple playbook to demonstrate the inventory_hostname and ansible_hostname

In this playbook, we have two tasks with debug module to display the value of our hostname variables.

---
 - name: "Playbook to test the inventory_hostname and ansible_hostname"
   hosts: testserver
   tasks:

    - name: What is my inventory_hostname
      debug: var={{inventory_hostname}}

    - name: What is my ansible_hostname
      debug: var={{ansible_hostname}}
      

 

Here is the execution output of this playbook

ansible_hostname

If you refer the preceding output, you can notice the actual hostname of the remote host is different and it is shown as a result for the ansible_hostname but the inventory_hostname displays the hostname/ip we have referred in the hosts file.

If you disable the facts by adding the gathter_facts: no and re-run the same playbook.  Now you will get VARIALBE IS NOT DEFINED for ansible_hostname.  So it is clear that this variable is derived from the facts

 

Trying the playbook with IP address in the ansible hosts file

Now let us modify our ansible_hosts file and replace the hostname with IPaddress. Let us retry the execution to see what values are displayed.

As mentioned earlier. inventory_hostname would display the IP address since it was mentioned in the ansible hosts file while the ansible_hostname result would remain unchanged

inventory_hostname

 

Using ansible_hostname and inventory_hostname in ansible ad_hoc commands

In this section, we are going to use these variables in ansible ad_hoc commands and observe the results.

inventory_hostname

In the preceding screenshot, you can see that we have two ad hoc commands with shell module to display the variables. As you can see the ansible_hostname is undefined while the inventory_hostname is working fine.

 

Conclusion

While both inventory_hostname and ansible_hostname both have their own use cases both are useful in our ansible automation but we have to choose the right one for our requirement.

since we covered the hostname in this article briefly, I would like to recommend you another article I have written earlier about the IP address and how to get IP address in ansible.

Ansible get ip address of current host or target

 

 

Do you have any requirements where you need our professional assistance write an email to [email protected] .

We like challanges just the way you do.

 

 

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading