

In the output, you see like below, which means files have been created on target hosts:Īnsible-playbook ansible_inventory_hostname_create_file.yaml -i custome_inventory.ini name: Here we are copying the Jinja2 template to remote machines name: Here we are checking ansible_hostname For this, we created a sample inventory file with the below contents. In this example, we will try to see the difference between the ansible_hostname and inventory_hostname variable.

On these local and target machines, we will try to do some changes using Ansible. As target machines we have two machines with hostnames as host-one (RHEL based machines) and host-two-ubuntu (Ubuntu based machines). In the lab, there is an ansible-controller server where we are running our commands and where inventory file resides. We will take some examples, but before going there, we should first understand our lab, we used for testing purpose. 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. Otherwise, Ansible cannot understand it and refuse to connect. 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.This variable can refer to an alias name if you have defined it in Ansible inventory file and set other related variables against it.Now below are some important points to note when working with this variable: The inventory file can be a customized one, on customized path or the default one viz. 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. How does Ansible inventory_hostname work?Īnsible Magic Variable inventory_hostname is a variable indeed, so it issued likewise in a playbook. 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. 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. There is another version of it named as inventory_hostname_short, which is used to shorten the long hostnames.
#Finale inventory enable connection software
Web development, programming languages, Software testing & others
#Finale inventory enable connection free
Start Your Free Software Development Course In all such cases, inventory_hostname has the real use. 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. 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. 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. Ansible inventory_hostname is among the highly used Magic Variables set, that also includes groups, hostvars, group_names. 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. Introduction to Ansible inventory_hostnameĪnsible 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.
