Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between inventory_hostname and ansible_hostname

Tags:

ansible

All I could find was this from the docs:

Additionally, inventory_hostname is the name of the hostname as configured in Ansible’s inventory host file. This can be useful for when you don’t want to rely on the discovered hostname ansible_hostname or for other mysterious reasons. If you have a long FQDN, inventory_hostname_short also contains the part up to the first period, without the rest of the domain.

Is there any actual difference between inventory_hostname and ansible_hostname variables in Ansible? If so, then which one should I use and when?

like image 874
Babken Vardanyan Avatar asked Aug 27 '17 18:08

Babken Vardanyan


People also ask

What is Inventory_hostname?

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.

What is Hostvars?

With hostvars , you can access variables defined for any host in the play, at any point in a playbook. You can access Ansible facts using the hostvars variable too, but only after you have gathered (or cached) facts.

What is Ansible_play_batch?

ansible_play_batch will return the hostname of a managed node, just like the Linux hostname command, in the form of an array of strings. ansible_play_hosts_all will return the hostname of a managed node, just like the Linux hostname command, in the form of an array of strings.

What are inventory variables in ansible?

The preferred practice in Ansible is actually not to store variables in the main inventory file. In addition to storing variables directly in the INI file, host and group variables can be stored in individual files relative to the inventory file. These variable files are in YAML format. Valid file extensions include '.


1 Answers

  • inventory_hostname - As configured in the ansible inventory file (eg: /etc/ansible/hosts). It can be an IP address or a name that can be resolved by the DNS
  • ansible_hostname - As discovered by ansible. Ansible ssh's into the host and gathers some facts. As part of the fact, it also discovers its hostname which is stored in ansible_hostname.

Which one should you use?

hostvars is a dictionary which has an entry for each inventory host. If you want to access host information, you need to use the inventory_hostname. If you want to use/print the name of the host as configured on the host, you should use ansible_hostname since most likely the IP will be used in the inventory file.

Important: To use ansible_hostname, you need to gather facts:

gather_facts: true 

Otherwise, you will get a message that ansible_hostname is not defined.

"ansible_hostname": "VARIABLE IS NOT DEFINED!" 

Try this with one host to understand the differences

  tasks:     - debug: var=inventory_hostname     - debug: var=ansible_hostname     - debug: var=hostvars 
like image 64
helloV Avatar answered Sep 21 '22 00:09

helloV