# Access variables for a host even if not executing a playbook against it

**URL:** <https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108>\
**Category:** Ansible Project\
**Created:** [November 28, 2012, 2:43am UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108 "2012-11-28T02:43:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_S](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@Lee\_S](https://forum.ansible.com/u/Lee_S)\
**Post date:** [November 28, 2012, 2:43am UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108/1 "2012-11-28T02:43:17Z")

</div>

I’ve set up Ansible to write out /etc/hosts files, so I want to know about all the hosts in my system. Here’s what’s in my template for /etc/hosts:

{% for key, host in hostvars.iteritems() %}  
{% if hostvars[key][‘internal\_ip’] %}  
{{ hostvars[key][‘internal\_ip’] }} {{ hostvars[key][‘name’] }}  
{% endif %}  
{% endfor %}

My hosts ini file defines ‘name’ and ‘internal\_ip’ for each host. The problem is if I execute a playbook on a subset of hosts, such as when I’m setting up a new server, hostvars only contains data about the hosts the playbooks is running on, so I have no way to access any information about them to write to /etc/hosts.

Is there a way to get all the variables, regardless of what hosts the playbook is being applied to? (Obviously I don’t care about the facts Ansible assembles, only the variables I defined myself)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 28, 2012, 2:49am UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108/2 "2012-11-28T02:49:33Z")

</div>

In order to access variables about other hosts you need to talk to  
them, but this can be as simple as a play that just speaks to them and  
does not do anything.

That hopefully answers your question. Let us know if not.

---

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [November 28, 2012, 7:24am UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108/3 "2012-11-28T07:24:06Z")

</div>

Hi,

> Is there a way to get all the variables, regardless of what hosts the  
> playbook is being applied to? (Obviously I don't care about the facts  
> Ansible assembles, only the variables I defined myself)

It is possible. Hostvars is modified dict that looks up variables from  
other hosts as needed.

> {% for key, host in hostvars.iteritems() %}  
> {% if hostvars[key]['internal\_ip'] %}  
> {{ hostvars[key]['internal\_ip'] }} {{ hostvars[key]['name'] }}  
> {% endif %}  
> {% endfor %}

That should be something like:

{% for host in groups['\<groupname'] -%}  
{% if hostvars[host].internal\_ip %}  
{{ hostvars[host].internal\_ip }} {{ hostvars[host].name }}  
{% endif %}  
{% endfor -%}

Greetings,

Jeroen

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 28, 2012, 12:59pm UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108/4 "2012-11-28T12:59:32Z")

</div>

Yes, this is true with respect to variables. It doesn't get the  
remote facts if it hasn't talked to the host yet (and should not).

Thanks for clarifying for everyone!

---

<div class="post-metadata">

**Author:** ![Lee\_S](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@Lee\_S](https://forum.ansible.com/u/Lee_S)\
**Post date:** [November 29, 2012, 8:49pm UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108/5 "2012-11-29T20:49:41Z")

</div>

Your solution works for a single group. I’ve found if I want to iterate through all the hosts, I have to do this in my /etc/hosts template

# All servers

{% for host in groups[‘all’] %}  
{% if hostvars[host].internal\_ip %}  
{{ hostvars[host].internal\_ip }} {{ hostvars[host].name }}  
{% endif %}  
{% endfor %}

Is there a more elegant way to iterate through all the hosts defined in the hosts file? I’d have expected I could use {% for host in hosts %} or something like that.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 29, 2012, 9:12pm UTC](https://forum.ansible.com/t/access-variables-for-a-host-even-if-not-executing-a-playbook-against-it/12108/6 "2012-11-29T21:12:35Z")

</div>

groups['all'] is how it's done.
