# Get info from a host, and use it in another host's template

**URL:** <https://forum.ansible.com/t/get-info-from-a-host-and-use-it-in-another-hosts-template/26773>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [February 17, 2018, 8:44pm UTC](https://forum.ansible.com/t/get-info-from-a-host-and-use-it-in-another-hosts-template/26773 "2018-02-17T20:44:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brigzzy\_Briggs](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Brigzzy\_Briggs](https://forum.ansible.com/u/Brigzzy_Briggs)\
**Post date:** [February 17, 2018, 8:44pm UTC](https://forum.ansible.com/t/get-info-from-a-host-and-use-it-in-another-hosts-template/26773/1 "2018-02-17T20:44:06Z")

</div>

Hi all,

I’m learning Ansible, and I’m trying to use it to template a Nagios config file. My network is 98% Debian and Ubuntu, with a FreePBX server to mix things up. There en lies the issue I’m having. When I run this command to check facts:

`  
ansible [freepbx.domain.com](http://freepbx.domain.com) -m

`

I can see that

`  
“ansible\_os\_family”: “Sangoma”

`

In my template, I have the following if statement:

`  
{% if ansible\_os\_family == “Debian” or ansible\_os\_family == “Ubuntu” %}  
define service{  
use local-service  
host\_name {{ hostvars[item].inventory\_hostname }}  
service\_description Apt Status  
check\_command check\_apt  
}  
{% endif %}

`

and the issue is that the block is still being generated for that server, even though it’s not Debian or Ubuntu.

I posted this on [Reddit as well](https://www.reddit.com/r/ansible/comments/7y5uje/troubles_with_an_if_statement_in_a_jinja2_template/?st=jdrthmo2&sh=38998e5f), and I realized that the issue is that the template is being applied to my Nagios server, which is Debian, so the ansible\_os\_family is always coming back Debian. What I’m having trouble with is how to get the if statement to get it’s facts from the target server, and not the one the playbook is running on. I’m able to get the hostname from the static hosts file like this

`  
{{ hostvars[item].inventory\_hostname }}

`

but I’m not having any luck figuring out how to get the template to apply to the values from that host. Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 17, 2018, 9:21pm UTC](https://forum.ansible.com/t/get-info-from-a-host-and-use-it-in-another-hosts-template/26773/2 "2018-02-17T21:21:42Z")

</div>

Without the more of the code on where and how you are generating the template it's difficult to say, but I believe you are looking for this

{% if hostvars[item].ansible\_os\_family == "Debian" or hostvars[item].ansible\_os\_family == "Ubuntu" %}

or the more cleaner version

{% if hostvars[item].ansible\_os\_family in ['Debian', 'Ubuntu'] %}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 20, 2018, 6:46am UTC](https://forum.ansible.com/t/get-info-from-a-host-and-use-it-in-another-hosts-template/26773/3 "2018-02-20T06:46:09Z")

</div>

You don't have access to the facts the hosts in group nagios-monitored, that's the reason it fails.

So you need to enable fact caching  
[http://docs.ansible.com/ansible/latest/playbooks\_variables.html#fact-caching](http://docs.ansible.com/ansible/latest/playbooks_variables.html#fact-caching)

or gather the facts before you run the nagios-server play.  
Facts gathered in one play is available in subsequent plays as long as they are in the same playbook.

---

<div class="post-metadata">

**Author:** ![Brigzzy\_Briggs](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Brigzzy\_Briggs](https://forum.ansible.com/u/Brigzzy_Briggs)\
**Post date:** [February 20, 2018, 5:16pm UTC](https://forum.ansible.com/t/get-info-from-a-host-and-use-it-in-another-hosts-template/26773/4 "2018-02-20T17:16:27Z")

</div>

That did it! Thanks Kai!

> 

So it seems that gathering facts is what populates those variables. This all makes sense now. Thanks so much for your guidance!

Cheers,

Danny
