Home Devops How to access hostvars in Ansible

How to access hostvars in Ansible

by The Linux Digest Guy
Ansible logo

Recently, I have been using Ansible to configure hosts that depend on the settings of other hosts in the play. This is a perfect use case for the magic variable called hostvars. Using hostvars you can access variables from any host that is in the play and use that information in the host you are currently working on. After researching this for one of my projects I decided to write up a little tutorial on how to access hostvars. Hopefully, you will find this useful in your own ansible projects.

Get hostvars in playbooks

To access hostvars you call on the hostvars magic variable. Which is a nested list. So to access the variable application_install_path on the host foo.mydomain.com, you would use hostvars[‘foo.mydomain.com’][‘application_install_path’].

You can also use hostvars to get Ansible facts for other hosts. One instance this could be useful is to get the IP address of a certain server so this server knows where to connect. This could be achieved like this:

"{{ hostvars['ansible_facts']['eth0']['ipv4']['address'] }}"

Hostvars from multiple hosts

You can also loop through multiple hosts to get information for each of them. In this example we will get the IP addresses of all the hosts in our play:

{% for host in ansible_play_hosts %}
   {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
{% endfor %}

You can also do the same for hosts that match a certain group.

{% for host in groups['database_servers'] %}
   {{ hostvars[host]['ansible_facts']['ansible_hostname'] }}
{% endfor %}

How to list all available hostvars

If you don’t know the name of the variable you are looking for, you can print out all the available hostvars for a particular host. Use the debug module from a playbook like this:

- debug:
  var: hostvars['testserver.linuxdigest.com']

Alternatively you can discover all variables for a given host directly from the command line:

ansible testserver.linuxdigest.com -u root -m setup

When not to use hostvars

When I was researching hostvars, I noticed that there is sometimes a little bit of confusion about what the hostvars variable is used for. This might be because variables for a given host are often called host variables.

When you are trying to get a variable for the host that is currently being worked on you don’t need to use the hostvars variable. You can just call the variable using curly brackets. Like in this example:

- file:
        path: "{{ application_install_path }}"
        state: directory

If you need to get a value from an embedded list like ansible_facts:

"{{ ansible_facts['eth0']['ipv4']['address'] }}"

Conclusion

When you need to get the value of variables from other hosts in the inventory, you can use the hostvars magic variable. There is a little bit of information about magic variables in the Ansible user guide.

I hope this short tutorial helps. I plan to write more tutorials about ansible in the coming months. So stay tuned and don’t forget to bookmark LinuxDigest.com.

2

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy