# Gather facts with delegate\_to

**URL:** <https://forum.ansible.com/t/gather-facts-with-delegate-to/17096>\
**Category:** Ansible Project\
**Created:** [December 18, 2014, 8:55am UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096 "2014-12-18T08:55:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antoine\_Rouaze](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Antoine\_Rouaze](https://forum.ansible.com/u/Antoine_Rouaze)\
**Post date:** [December 18, 2014, 8:55am UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/1 "2014-12-18T08:55:51Z")

</div>

Hello,

I’m trying to use the setup module with delegation, but I don’t understand one thing. Here is my test playbook :

`

- hosts: webservers  
tasks:

- debug: var=ansible\_hostname

- name: Get sql master facts  
setup:  
delegate\_to: “mrk-test-sql1”  
register: sql\_master\_facts

- debug: var=sql\_master\_facts.ansible\_facts.ansible\_default\_ipv4.address

- debug: var=ansible\_hostname  
`

I just want to retrieve the private ip of my master db with Ansible facts and add it in the /etc/hosts file of my web servers. Here is the output :

`  
PLAY [webservers] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [mrk-test-web1]  
ok: [mrk-test-web2]

TASK: [debug var=ansible\_hostname] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [mrk-test-web1] =\> {  
“ansible\_hostname”: “mrk-test-web1”  
}  
ok: [mrk-test-web2] =\> {  
“ansible\_hostname”: “mrk-test-web2”  
}

TASK: [Get sql master facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [mrk-test-web2 → mrk-test-sql1]  
ok: [mrk-test-web1 → mrk-test-sql1]

TASK: [debug var=sql\_master\_facts.ansible\_facts.ansible\_default\_ipv4.address] \*\*\*  
ok: [mrk-test-web1] =\> {  
“sql\_master\_facts.ansible\_facts.ansible\_default\_ipv4.address”: “10.240.16.26”  
}  
ok: [mrk-test-web2] =\> {  
“sql\_master\_facts.ansible\_facts.ansible\_default\_ipv4.address”: “10.240.16.26”  
}

TASK: [debug var=ansible\_hostname] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [mrk-test-web1] =\> {  
“ansible\_hostname”: “mrk-test-sql1”  
}  
ok: [mrk-test-web2] =\> {  
“ansible\_hostname”: “mrk-test-sql1”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
mrk-test-web1 : ok=5 changed=0 unreachable=0 failed=0  
mrk-test-web2 : ok=5 changed=0 unreachable=0 failed=0  
`

All work, but I don’t understand the result of the latest task. Why is it not like this (like the result of the first task) :

`TASK: [debug var=ansible_hostname] ******************************************** ok: [mrk-test-web1] => { "ansible_hostname": "mrk-test-web1" } ok: [mrk-test-web2] => { "ansible_hostname": "mrk-test-web2" }`

Normally, gathering facts with delegate should not overwrite the current facts of my web servers ?

My workaround to fix the problem is to recall “setup” for my web server.

Thanks.

Regards,  
Antoine Rouaze

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 18, 2014, 2:59pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/2 "2014-12-18T14:59:23Z")

</div>

> Normally, gathering facts with delegate should not overwrite the current  
> facts of my web servers ?

that is exactly what is happening, you are gathering facts on the  
delegated server but assigning them to the current server  
(inventory\_hostname).

---

<div class="post-metadata">

**Author:** ![Antoine\_Rouaze](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Antoine\_Rouaze](https://forum.ansible.com/u/Antoine_Rouaze)\
**Post date:** [December 18, 2014, 3:06pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/3 "2014-12-18T15:06:16Z")

</div>

But the retrieved facts should not just stay inside the delegate scope ? I don’t understand why gathering facts on a delegated server assigning them to the current server ?

Antoine Rouaze

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 18, 2014, 3:22pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/4 "2014-12-18T15:22:01Z")

</div>

delegate\_to just executes the current task on a different target, the  
returns of any delegated task is still the 'current host'.

---

<div class="post-metadata">

**Author:** ![Antoine\_Rouaze](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Antoine\_Rouaze](https://forum.ansible.com/u/Antoine_Rouaze)\
**Post date:** [December 18, 2014, 3:28pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/5 "2014-12-18T15:28:45Z")

</div>

Ok, so do you know any other way to retrieve facts from host not included in the play ?

Antoine Rouaze

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 18, 2014, 3:33pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/6 "2014-12-18T15:33:34Z")

</div>

Currently, 2 ways:

- create a first play that just gathers on all hosts, it can be in  
same file as the current play or not, but it needs to be run on the  
same ansible invocation.  
- enabled fact caching, you just have to have contacted that host  
withing the cache lifetime.

---

<div class="post-metadata">

**Author:** ![Antoine\_Rouaze](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Antoine\_Rouaze](https://forum.ansible.com/u/Antoine_Rouaze)\
**Post date:** [December 18, 2014, 3:44pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/7 "2014-12-18T15:44:49Z")

</div>

Ok, with these two ways I can get the facts like this “hostvars[‘mrk-test-sql1’]”

Thanks for the help !

Antoine Rouaze

---

<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:** [December 18, 2014, 5:05pm UTC](https://forum.ansible.com/t/gather-facts-with-delegate-to/17096/8 "2014-12-18T17:05:07Z")

</div>

The idiomatic way is:

- hosts: thisserver  
tasks: 

- hosts: webservers  
tasks:
