Ansible Inventory_hostname & ansible_hostname Examples | Devops Junc (2024)

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.

Table of Contents

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.7058.43.89.21[vagrantservers]mwiapp01.example.commwiapp02.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_hostnameinventory_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 machineinventory_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 playbookinventory_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 machineSince 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 commandAvailable 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

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

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.

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 [emailprotected] .

We like challanges just the way you do.

Cheers
Sarav AK

Ansible Inventory_hostname & ansible_hostname Examples | Devops Junc (6)

Follow me on Linkedin My ProfileFollow DevopsJunction onFacebook orTwitterFor more practical videos and tutorials. Subscribe to our channel

Ansible Inventory_hostname & ansible_hostname Examples | Devops Junc (7)

Signup for Exclusive "Subscriber-only" Content

More from Middleware Inventory

  • Ansible Playbook Examples - Sample Ansible Playbooks | Devops Junction

    In this post, we are going to see examples of Ansible playbook and various different modules and playbook examples with various modules and multiple hosts. We will start with a basic Ansible playbook and learn what is task and play and what is playbook etc. What is Ansible Playbook It…

  • Ansible Find Examples - How to use Ansible Find

    Ansible find module functions as same as the Linux Find command and helps to find files and directories based on various search criteria such as age of the file, accessed date, modified date, regex search pattern etcetera. As said earlier, this is more of an ansible way to execute the…

  • Add SSH Key to EC2 instances with Ansible - Automated

    Whether it is On-Prem (or) Cloud-hosted, A Typical Non-Containerized Enterprise infrastructure would have ample of Virtual Machines aka Servers [ Linux ] Let us suppose that you work for the DevOps team of a Big Organization where you manage 100+ ec2 instances. You have a new hire in your team…

  • Ansible Shell Module Examples

    Ansible shell module is designed to execute Shell commands against the target Unix based hosts. Unlike the Ansible command module, Ansible Shell would accept any highly complexed commands with pipes, redirection etc and you can also execute Shell scripts using Ansible Shell module. The Advantage of Ansible Shell module…

  • Ansible EC2 Example - Create EC2 instance with Ansible

    Infrastructure as Code is getting all attention it deserves and everyone is trying to find their way to the `Completely automated Infrastructure Provisioning & Management` While there are a lot of tools available now in the market starting from Terraform, AWS CloudFormation, Chef, Puppet, Salt Stack There are some differences…

Ansible Inventory_hostname & ansible_hostname Examples | Devops Junc (2024)
Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5605

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.