# How to retrieve facts about host B inside a playbook which run for host A

**URL:** <https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032>\
**Category:** Ansible Project\
**Created:** [April 16, 2014, 2:07pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032 "2014-04-16T14:07:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [April 16, 2014, 2:07pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/1 "2014-04-16T14:07:35Z")

</div>

Hello friends!  
Almost sure that this is my fault but at this time is still obscure for me how to retrieve facts about host B inside a playbook which run for host A.

This is the real case: I want to populate Icinga host definition running the playbook on host A, but retrieving facts about monitored host B:

Already tried [http://docs.ansible.com/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts) (templates/host\_def.j2) :

define host{  
use generic-host  
host {{ hostvars[‘hostB’][‘ansible\_fqdn’] }}  
address {{ hostvars[‘hostB’][‘ansible\_eth0.ipv4.address’] }}  
max\_check\_attempts 5  
BLABLA  
}

but this give me an error. Is this the right way to use magic variables?

Any pointing? Thanks!

---

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [April 16, 2014, 3:30pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/2 "2014-04-16T15:30:29Z")

</div>

More details.

vars.yml  
fqdn: “{{ hostvars[hostB][‘ansible\_fqdn’] }}”  
address: “{{ hostvars[hostB][‘ansible\_eth0.ipv4.address’] }}”

---- separator -----

templates/host\_def.j2  
define host{  
use generic-host  
host\_name {{ fqdn }}  
address {{ address }}  
max\_check\_attempts 5  
check\_command check-host-alive  
}

---- separator -----

task.yml

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [April 16, 2014, 4:49pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/3 "2014-04-16T16:49:47Z")

</div>

Valentino Gagliardi \<servermanaged@gmail.com\> napisał:

> More details.
> 
> vars.yml  
> fqdn: "{{ hostvars[hostB]['ansible\_fqdn'] }}"  
> address: "{{ hostvars[hostB]['ansible\_eth0.ipv4.address'] }}"

If hostB is the inventory name of that host (and not a variable) use 'hostB' in quotes.

---

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [April 16, 2014, 5:31pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/4 "2014-04-16T17:31:21Z")

</div>

hostB is defined in inventory. Unfortunately does not single quote nor strong quoting solve the issue:

fqdn: “{{ hostvars[‘hostB’][‘ansible\_fqdn’] }}”  
address: “{{ hostvars[‘hostB’][‘ansible\_eth0’][‘ipv4’][‘address’] }}”

Error:

fatal: [mon] =\> One or more undefined variables: ‘dict object’ has no attribute ‘ansible\_fqdn’

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [April 16, 2014, 5:48pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/5 "2014-04-16T17:48:25Z")

</div>

Valentino Gagliardi \<servermanaged@gmail.com\> napisał:

> hostB is defined in inventory. Unfortunately does not single quote nor  
> strong quoting solve the issue:
> 
> fqdn: "{{ hostvars['hostB']['ansible\_fqdn'] }}"  
> address: "{{ hostvars['hostB']['ansible\_eth0']['ipv4']['address'] }}"
> 
> Error:
> 
> fatal: [mon] =\> One or more undefined variables: 'dict object' has no  
> attribute 'ansible\_fqdn'

That probably means you have not yet gathered facts from that host.

I'm not sure how running action: setup with delegate\_to would act, but if that doesn't work you can put a dummy play at the beginning of your playbook.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 16, 2014, 6:19pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/6 "2014-04-16T18:19:32Z")

</div>

adding a ‘gather play’ before your main play would work

- hosts: hostB  
gather\_facts: True

- hosts: hostA  
…  
​

---

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [April 16, 2014, 6:58pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/7 "2014-04-16T18:58:35Z")

</div>

Hello Brian  
Just added a gather\_facts: yes for hostB and it worked.  
Thanks all.

---

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [April 16, 2014, 7:49pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/8 "2014-04-16T19:49:09Z")

</div>

I’m sorry but there’s another question. This time I want to access {{ hostvars[host][‘ansible\_fqdn’] }} within a task:

- name: Copy Host Definition (Server)  
template: src=templates/host\_def.j2 dest=/etc/icinga/servers/{{ hostvars[host][‘ansible\_fqdn’] }}.cfg owner=root group=root mode=0644  
notify:
- restart icinga  
tags:
- server\_host

This task don’t work. There’s a way to access ansible\_fqdn?

Regards.

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 16, 2014, 11:45pm UTC](https://forum.ansible.com/t/how-to-retrieve-facts-about-host-b-inside-a-playbook-which-run-for-host-a/15032/9 "2014-04-16T23:45:07Z")

</div>

Well, what is the value of “host”?

It’s a variable – is it defined? Does it have a sane value?

If you’re looking for the value of the “current” host, you can reference ‘ansible\_fqdn’ directly: {{ ansible\_fqdn }}. Otherwise, make sure that host is defined and holds the value it’s supposed to with the **debug** module.
