EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Ansible inventory_hostname

Home » Software Development » Software Development Tutorials » Ansible Tutorial » Ansible inventory_hostname

Ansible inventory_hostname

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,629 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

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:

Ansible inventory_hostname 1

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:

using Jinja2 templating

On checking the contents of these files on remote hosts:

Ansible inventory_hostname 3

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 –

  1. Ansible Yum Module
  2. Ansible Conditional
  3. Ansible local_action
  4. Ansible Apt

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Ansible Tutorial
  • Ansible Basics
    • What is Ansible
    • Is Ansible free
    • How to Install Ansible
    • Ansible Versions
    • Ansible Architecture
    • Ansible Commands
    • Ansible ad-hoc Commands
    • Ansible Playbooks
    • Ansible Roles
    • Ansible Tags
    • Ansible Ping
    • Ansible Apt
    • Ansible Facts
    • Ansible Tower
    • Ansible Galaxy
    • Ansible Handlers
    • Ansible Loop
    • Ansible Block
    • Ansible Conditional
    • Ansible YAML
    • Ansible Debug
    • Ansible Synchronize
    • Ansible Template
    • Ansible group_vars
    • Ansible Lookup
    • Ansible File Module
    • Ansible Yum Module
    • Ansible Shell Module
    • Ansible lineinfile
    • Ansible Service Module
    • Ansible User Module
    • Ansible Windows Modules
    • ansible-doc
    • Ansible Filters
    • Ansible Add User to Group
    • Ansible Register
    • Ansible Set Fact
    • Ansible Hosts File
    • Ansible add_host
    • Ansible Collections
    • Ansible with_items
    • Ansible Replace Line in File
    • Ansible inventory_hostname
    • Ansible Dynamic Inventory
    • Ansible local_action
    • Ansible Firewalld
    • Ansible Unarchive
    • Ansible Sudo
    • Ansible Create Directory
    • Ansible Reboot
    • Ansible wait_for
    • Ansible run_once
    • Ansible Authorized_key
    • Ansible Interview Questions

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Linux Training Course Learn More