# Getting facts from hosts that are not the target of a playbook

**URL:** <https://forum.ansible.com/t/getting-facts-from-hosts-that-are-not-the-target-of-a-playbook/13010>\
**Category:** Ansible Project\
**Created:** [June 23, 2013, 5:31am UTC](https://forum.ansible.com/t/getting-facts-from-hosts-that-are-not-the-target-of-a-playbook/13010 "2013-06-23T05:31:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paulo\_Tioseco](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Paulo\_Tioseco](https://forum.ansible.com/u/Paulo_Tioseco)\
**Post date:** [June 23, 2013, 5:31am UTC](https://forum.ansible.com/t/getting-facts-from-hosts-that-are-not-the-target-of-a-playbook/13010/1 "2013-06-23T05:31:21Z")

</div>

Hello Ansible Experts,

How would I get a fact/variable from a host that is not the target of my playbook?

I know that hostvars exist for this reason but since the hosts that I want to get variables from are not the target of my playbook, the hostvars for that host is not populated.

For this scenario, I am generating a configuration file for a Munin master node which needs to get the ansible\_processor\_count from the other nodes. This playbook is executed for the master node only so therefore only the facts for that master node are gathered.

Here’s the Jinja template that I am trying to render:

{% for host in groups[‘all’] %}  
[{{ host }}]  
address {{ host }}  
use\_node\_name yes  
cpu.user.warning {{ hostvars[host][‘ansible\_processor\_count’] \* 100 \* 0.8 }}  
cpu.user.critical {{ hostvars[host][‘ansible\_processor\_count’] \* 100 \* 0.9 }}  
{% endfor %}

I get a:

fatal: [munin-master] =\> {‘msg’: “‘dict object’ has no attribute ‘ansible\_processor\_count’”, ‘failed’: True}

Thank you!

Paulo

---

<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:** [June 23, 2013, 5:32pm UTC](https://forum.ansible.com/t/getting-facts-from-hosts-that-are-not-the-target-of-a-playbook/13010/2 "2013-06-23T17:32:31Z")

</div>

Sure, basically you just need a dummy play that talks to the host prior to referencing it, like so:

- hosts: other\_hosts # you could put ‘all’ here if you want to, even  
tasks: 

- hosts: main\_group\_of\_hosts  
tasks:

- your regular tasks from above go here

Once you will have already talked to those other hosts, their facts will be available via hostvars.

---

<div class="post-metadata">

**Author:** ![Lionel\_Lindemann](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@Lionel\_Lindemann](https://forum.ansible.com/u/Lionel_Lindemann)\
**Post date:** [June 26, 2013, 8:31am UTC](https://forum.ansible.com/t/getting-facts-from-hosts-that-are-not-the-target-of-a-playbook/13010/3 "2013-06-26T08:31:52Z")

</div>

Hello,

It seems ok in a regular playbook, since the dummy call would be in the same file or context.

But I believe this is not very clean when required from a role: having a reusable piece of functionality in differing contexts assume a dummy play was called seems error prone.

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 26, 2013, 1:20pm UTC](https://forum.ansible.com/t/getting-facts-from-hosts-that-are-not-the-target-of-a-playbook/13010/4 "2013-06-26T13:20:08Z")

</div>

The code path is reliable.

You just need do to something like

- name: ‘gather facts on all hosts’  
hosts: all  
tasks: 

- name: “do stuff”  
hosts: whatever  
roles:

- …

In any event, we’ll probably consider caching facts in a future release, although you would still need to talk to each host at least once.
