Using facts from all systems

681 views
Skip to first unread message

Matt Hughes

unread,
Sep 5, 2014, 5:35:56 PM9/5/14
to ansible...@googlegroups.com
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"



Matt Hughes

unread,
Sep 5, 2014, 8:30:31 PM9/5/14
to ansible...@googlegroups.com
This is what I wanted: 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 %}

Michael DeHaan

unread,
Sep 6, 2014, 9:52:34 AM9/6/14
to ansible...@googlegroups.com
On Fri, Sep 5, 2014 at 5:35 PM, Matt Hughes <hughe...@gmail.com> wrote:
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.


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"] }}



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"



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/35b9a11f-9654-458d-8220-22218cbec508%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rek...@voodoowarez.com

unread,
Sep 6, 2014, 2:51:51 PM9/6/14
to ansible...@googlegroups.com
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.

On Fri, Sep 05, 2014 at 02:35:55PM -0700, Matt Hughes wrote:
> 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.

Michael DeHaan

unread,
Sep 6, 2014, 2:52:59 PM9/6/14
to ansible...@googlegroups.com
On Fri, Sep 5, 2014 at 7:56 PM, <rek...@voodoowarez.com> wrote:
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


 

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.

On Fri, Sep 05, 2014 at 02:35:55PM -0700, Matt Hughes wrote:
> 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.

> 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"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages