# Using facts from all systems

**URL:** <https://forum.ansible.com/t/using-facts-from-all-systems/16133>\
**Category:** Ansible Project\
**Created:** [September 5, 2014, 9:35pm UTC](https://forum.ansible.com/t/using-facts-from-all-systems/16133 "2014-09-05T21:35:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [September 5, 2014, 9:35pm UTC](https://forum.ansible.com/t/using-facts-from-all-systems/16133/1 "2014-09-05T21:35:55Z")

</div>

As far as I understand it, fact variables are exposed for the _current_ server that Ansible is operating on. So if I say: {{ ansible\_default\_ipv4.address }}, that is going to get evaluated for each host as I perform some operation using that variable.

I want that same variable, but for all hosts in the current play. Say I had three hosts I was operating against:

one: 98.0.0.1  
two: 98.0.0.2  
three: 98.0.0.3

I want to be able to do something like:

hosts={% for fact in facts %}  
{{ fact[‘ansible\_default\_ipv4.address’] }},  
{% endfor %}

and have that evaluated to “98.0.0.1,98.0.0.2,98.0.0.3”

---

<div class="post-metadata">

**Author:** ![rektide](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@rektide](https://forum.ansible.com/u/rektide)\
**Post date:** [September 5, 2014, 11:56pm UTC](https://forum.ansible.com/t/using-facts-from-all-systems/16133/2 "2014-09-05T23:56:12Z")

</div>

hostvars['somehost']['ansible\_default\_ipv4'].address

inventory\_hostname is a var worth mentioning: it's the hostname of the local system. useful for:  
hostvars[inventory\_hostname]['ansible\_default\_ipv4']

there's also a 'groups' top-level var you can use to iterate through the inventory, which may or may not be  
better than iterating through hostvars to get your list of hosts. pardon, been a couple months since i touched  
that particular use-case.

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [September 6, 2014, 12:30am UTC](https://forum.ansible.com/t/using-facts-from-all-systems/16133/3 "2014-09-06T00:30:31Z")

</div>

This is what I wanted: [https://github.com/ansible/ansible/issues/196](https://github.com/ansible/ansible/issues/196) So the resulting template code was:

hosts={% for k,v in hostvars.iteritems() %}{{v[‘ansible\_default\_ipv4’][‘address’]}},{% endfor %}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 6, 2014, 1:52pm UTC](https://forum.ansible.com/t/using-facts-from-all-systems/16133/4 "2014-09-06T13:52:28Z")

</div>

> As far as I understand it, fact variables are exposed for the \*current\*  
> server that Ansible is operating on. So if I say: {{  
> ansible\_default\_ipv4.address }}, that is going to get evaluated for each  
> host as I perform some operation using that variable.

Not the case, actually.

Facts are available for all servers in the current play or talked to in any  
previous play \*OR\* potentially all servers previously talked to if "fact  
caching" is enabled.

[http://docs.ansible.com/playbooks\_variables.html#fact-caching](http://docs.ansible.com/playbooks_variables.html#fact-caching)

Facts for other systems can be accessed like so using the "hostvars"  
variable:

{{ hostvars["hostname.example.com"]["fact\_name"] }}

For example:

{{ hostvars["hostname.example.com"]["ansible\_default\_ipv4"]["address"] }}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 6, 2014, 6:52pm UTC](https://forum.ansible.com/t/using-facts-from-all-systems/16133/5 "2014-09-06T18:52:55Z")

</div>

> hostvars['somehost']['ansible\_default\_ipv4'].address
> 
> inventory\_hostname is a var worth mentioning: it's the hostname of the  
> local system. useful for:  
> hostvars[inventory\_hostname]['ansible\_default\_ipv4']

This can be simplified just down to:

{{ ansible\_default\_ipv4 }}

however.

It can be useful for when you need to fish out a variable name  
programmatically though.

{{ hostvars[inventory\_hostname]["some\_variable"][name\_of\_hash\_key] }}

etc
