# Q: use of delegated\_facts?

**URL:** <https://forum.ansible.com/t/q-use-of-delegated-facts/25493>\
**Category:** Ansible Project\
**Created:** [September 15, 2017, 9:02am UTC](https://forum.ansible.com/t/q-use-of-delegated-facts/25493 "2017-09-15T09:02:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sieds\_Pallas](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Sieds\_Pallas](https://forum.ansible.com/u/Sieds_Pallas)\
**Post date:** [September 15, 2017, 9:02am UTC](https://forum.ansible.com/t/q-use-of-delegated-facts/25493/1 "2017-09-15T09:02:22Z")

</div>

Hi,  
I am getting confused on the working and purpose of delegated\_facts.  
If I read the manual correctly:

[http://docs.ansible.com/ansible/latest/playbooks\_delegation.html#delegated-facts](http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts)

when you delegate a task to another host than the current managed host from the inventory hosts: list, the ansible facts are gathered from the delegated host but assigned to the current inventory host.  
Specifying “delegate\_facts: True” would change this that the ansible facts are assigned to the delegated host.

I do not understand what they mean with that facts are assigned to a particular host. When you run a playbook, as it loops over all “hosts:”, facts are gathered and the ansible\_\* variables get some value valid for that iteration - so without some host qualifier.

The RedHat training manual DO407 (pp.295,296) is not sufficiently explicit about this but appears confused, but from their example I get the impression that it is the other way round.

When I try it myself I see:

- with just "delegate\_to: ", the ansible facts are gathered from the delegated host, which is what I would expect if you run a task on a delegated host;
- with in addition "delegated\_facts: ", the ansible facts are gathered instead from the current inventory host - which means that the name “delegated\_facts” is confusing.

So is the documentation wrong?  
How is delegated\_facts supposed to work?

Here’s the code and output:

\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<  
$ cat inventory  
[invhosts]  
node1  
[delegates]  
node2

$ cat testdelegate.yml

---

<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:** [September 17, 2017, 4:09pm UTC](https://forum.ansible.com/t/q-use-of-delegated-facts/25493/2 "2017-09-17T16:09:13Z")

</div>

> Hi,  
> I am getting confused on the working and purpose of delegated\_facts.  
> If I read the manual correctly:
> 
> [http://docs.ansible.com/ansible/latest/playbooks\_delegation.html#delegated-facts](http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts)
> 
> when you delegate a task to another host than the current managed host from  
> the inventory hosts: list, the ansible facts are gathered from the  
> delegated host but assigned to the current inventory host.  
> Specifying "delegate\_facts: True" would change this that the ansible facts  
> are assigned to the delegated host.

This is correct, and you example does show this.

> I do not understand what they mean with that facts are assigned to a  
> particular host. When you run a playbook, as it loops over all "hosts:",  
> facts are gathered and the ansible\_\* variables get some value valid for  
> that iteration - so without some host qualifier.

I don't now where you have this from, but I think they are referring to Ansible default behavior.  
The fist thing Ansible does is to collect from all host, you can see that in the output as "TASK [Gathering Facts]"

This behavior can be changed by the play directive "gather\_facts"

> The RedHat training manual DO407 (pp.295,296) is not sufficiently explicit  
> about this but appears confused, but from their example I get the  
> impression that it is the other way round.

Never seen it.

> When I try it myself I see:  
> - with just "delegate\_to: ", the ansible facts are gathered from the  
> delegated host, which is what I would expect if you run a task on a  
> delegated host;  
> - with in addition "delegated\_facts: ", the ansible facts are gathered  
> instead from the current inventory host - which means that the name  
> "delegated\_facts" is confusing.

I think you are being confused because Ansible gather\_facts is default true and facts is gathered from both hosts.

> So is the documentation wrong?

No, they are correct.

> How is delegated\_facts supposed to work?

As your example, but I gonna give you a example that might show this more clearly.

> Here's the code and output:
> 
> \<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<  
> $ cat inventory  
> [invhosts]  
> node1  
> [delegates]  
> node2
> 
> $ cat testdelegate.yml  
> ---  
> - name: test delegate functionality  
> &nbsp;&nbsp;&nbsp;hosts: invhosts  
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- name: setup  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setup:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: "{{ item }}"  
> # delegate\_facts: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ groups['delegates'] }}"  
> &nbsp;&nbsp;&nbsp;- name: show  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: msg="from fact of {{ inventory\_hostname }}, ansible\_hostname =  
> {{ ansible\_hostname }}.\n"  
> # debug: msg="from delegated fact of {{ inventory\_hostname }},  
> ansible\_hostname = {{ ansible\_hostname }}.\n"  
> ...
> 
> $ ansible-playbook testdelegate.yml  
> PLAY [test delegate functionality]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> TASK [Gathering Facts]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Here you see the default task where Ansible gather facts from all host.

Using your inventory try this code, maybe it show the feature more clear since i turn off the default fact gathering.
