# Delgate\_facts understanding

**URL:** <https://forum.ansible.com/t/delgate-facts-understanding/31589>\
**Category:** Ansible Project\
**Created:** [November 2, 2019, 9:57pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589 "2019-11-02T21:57:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [November 2, 2019, 9:57pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/1 "2019-11-02T21:57:50Z")

</div>

Hello everyone,

I don’t totally get the point of the **_delegate\_facts_** usage in Ansible.

I have written the follow code to test this clause:

> _- name: delegate facts testing_  
> _hosts: localhost_  
> _tasks:_
> 
> _- name: print FQDN - first attempt_  
> _debug:_  
> _msg: “{{ ansible\_fqdn }}”_
> 
> _- name: gather facts on remote host_  
> _setup:_  
> _delegate\_to: “{{ item }}”_  
> _with\_items: “{{ groups[‘anotherhost’] }}”_  
> **_delegate\_facts: false_**
> 
> _- name: print ansible FQDN - second attempt_  
> _debug:_  
> _msg: “{{ ansible\_fqdn }}”_

So… If I set DELEGATE\_FACTS to false, I get the following output:

→ PRINT FQDN - First attempt → LOCAL HOSTNAME  
 → PRINT FQDFN - Second attempt → ANOTHER HOSTNAME

- Is this because the default facts gathered at the beginning of the play (via setup localhost) , have been overwritten by the facts gathered on the ANOTHERHOST?

Otherwise, If I change delegate\_facts to true. I obtain LOCALHOSTNAME in both attempts.

- Is this because I ve assigned the default gathered facts to anotherhost’s facts?

Sorry for this silly question… but I cannot find an example good enough to understand this.

Thank you all in advance 🙂  
Vicente.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 2, 2019, 11:59pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/2 "2019-11-02T23:59:10Z")

</div>

> \*- name: delegate facts testing\*  
> \* hosts: localhost  
> \* tasks:  
> \* - name: print FQDN - first attempt  
> \* debug:  
> \* msg: "{{ ansible\_fqdn }}"  
> \* - name: gather facts on remote host  
> \* setup:  
> \* delegate\_to: "{{ item }}"  
> \* with\_items: "{{ groups['anotherhost'] }}"  
> \* delegate\_facts: false  
> \* - name: print ansible FQDN - second attempt  
> \* debug:  
> \* msg: "{{ ansible\_fqdn }}"
> 
> &nbsp;&nbsp;&nbsp;If I set DELEGATE\_FACTS to false, I get the following output:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--\> PRINT FQDN - First attempt --\> LOCAL HOSTNAME  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--\> PRINT FQDFN - Second attempt --\> ANOTHER HOSTNAME
> 
> &nbsp;&nbsp;&nbsp;Is this because the default facts gathered at the beginning of the  
> &nbsp;&nbsp;&nbsp;play (via setup localhost) , have been overwritten by the facts gathered on  
> &nbsp;&nbsp;&nbsp;the ANOTHERHOST?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YES.

> &nbsp;&nbsp;&nbsp;Otherwise, If I change delegate\_facts to true. I obtain LOCALHOSTNAME in  
> &nbsp;&nbsp;&nbsp;both attempts.
> 
> &nbsp;&nbsp;&nbsp;Is this because I ve assigned the default gathered facts to  
> &nbsp;&nbsp;&nbsp;anotherhost's facts?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YES.

For the reference: "The directive delegate\_facts may be set to True to assign  
the task’s gathered facts to the delegated host instead of the current one."  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_delegation.html#delegated-facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegated-facts)

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [November 3, 2019, 8:27pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/3 "2019-11-03T20:27:15Z")

</div>

Thank you very much Vlado.

But it means that you have to manage this clause carefully, because if you use delegate\_facts = FALSE (true by default) , the rest of your tasks after the delegated one will be using {{ ansible\_fqdn }} with a wrong hostname (different from the current managed host), which could lead to errors… =S

I don’t get the point of this functionality at all, any useful example that you have in mind?

Thank you for your help 🙂

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 3, 2019, 11:32pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/4 "2019-11-03T23:32:25Z")

</div>

> \>  
> \> \> \*- name: delegate facts testing\*  
> \> \> \* hosts: localhost  
> \> \> \* tasks:  
> \> \> \* - name: print FQDN - first attempt  
> \> \> \* debug:  
> \> \> \* msg: "{{ ansible\_fqdn }}"  
> \> \> \* - name: gather facts on remote host  
> \> \> \* setup:  
> \> \> \* delegate\_to: "{{ item }}"  
> \> \> \* with\_items: "{{ groups['anotherhost'] }}"  
> \> \> \* delegate\_facts: false  
> \> \> \* - name: print ansible FQDN - second attempt  
> \> \> \* debug:  
> \> \> \* msg: "{{ ansible\_fqdn }}"  
> \> \>  
> \> \> If I set DELEGATE\_FACTS to false, I get the following output:  
> \> \> --\> PRINT FQDN - First attempt --\> LOCAL HOSTNAME  
> \> \> --\> PRINT FQDFN - Second attempt --\> ANOTHER HOSTNAME  
> \> \>  
> \> \> Is this because the default facts gathered at the beginning of the  
> \> \> play (via setup localhost) , have been overwritten by the facts  
> \> \> gathered on  
> \> \> the ANOTHERHOST?  
> \>  
> \> YES.  
> \>  
> \> \> Otherwise, If I change delegate\_facts to true. I obtain LOCALHOSTNAME  
> \> \> in  
> \> \> both attempts.  
> \> \>  
> \> \> Is this because I ve assigned the default gathered facts to  
> \> \> anotherhost's facts?  
> \>  
> \> YES.  
> \>  
> \>  
> \> For the reference: "The directive delegate\_facts may be set to True to  
> \> assign  
> \> the task’s gathered facts to the delegated host instead of the current  
> \> one."  
> \>  
> \> [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_delegation.html#delegated-facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegated-facts)

> But it means that you have to manage this clause carefully, because if you  
> use delegate\_facts = FALSE (true by default) , the rest of your tasks  
> after the delegated one will be using {{ ansible\_fqdn }} with a wrong  
> hostname (different from the current managed host), which could lead to  
> errors... =S

&nbsp;&nbsp;YES. (... if you use "delegate\_facts: false" (false by default) ...)

&nbsp;&nbsp;To avoid the problem use "delegate\_facts: true" with the "setup" module.

> I don't get the point of this functionality at all, any useful example that  
> you have in mind?

&nbsp;&nbsp;The example in the doc is a good use-case  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_delegation.html#delegated-facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegated-facts)

&nbsp;&nbsp;- hosts: app\_servers  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: gather facts from db servers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setup:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: "{{item}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_facts: True  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{groups['dbservers']}}"

&nbsp;&nbsp;The play is running at app\_servers and knows nothing about the dbservers.  
&nbsp;&nbsp;The task will "setup" dbservers facts that can be used by app\_servers.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [November 4, 2019, 7:26am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/5 "2019-11-04T07:26:19Z")

</div>

Hello,

Are we sure that the delegate\_facts is set to “false” by default? As stated in the doc:

_“By default, any fact gathered by a delegated task are assigned to the (the current host) instead of the host which actually produced the facts (the delegated to host)”_

If the facts gathered by a delegated task are assigned to inventory\_hostame (CURRENT HOST), means that the **delegate\_facts = true** , isn’t it?  
As per my first code example, if you use **delegate\_facts = false** , you will have the delegated hosts’s facts instead of inventory\_hostnames’s (which are overridden). In the case of **delegate\_facts = true** , you will keep CURRENT inventory\_hostname’s facts along the whole play.

Thank you very much, sorry for bothering with this 🙂

By the way, your example makes sense… if you want to use DB Servers facts in APPServers tasks.

Thanks a lot,  
Vicente.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 4, 2019, 8:20am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/6 "2019-11-04T08:20:54Z")

</div>

> Are we sure that the delegate\_facts is set to "false" by default?

&nbsp;&nbsp;(I wonder how "we" can be sure? Kind of a collective experience?)

&nbsp;&nbsp;The default can be easily tested. For example the play

&nbsp;&nbsp;- hosts: test\_01  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- setup:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: test\_02  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_hostname

&nbsp;&nbsp;gives

&nbsp;&nbsp;ok: [test\_01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_hostname": "test\_01"  
&nbsp;&nbsp;}

&nbsp;&nbsp;The same play without the line "delegate\_facts: true" gives

&nbsp;&nbsp;ok: [test\_01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_hostname": "test\_02"  
&nbsp;&nbsp;}

> By the way, your example makes sense.. if you want to use DB Servers facts  
> in APPServers tasks.

&nbsp;&nbsp;It's not my example. It's been publicly available for a while.

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [November 4, 2019, 8:46am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/7 "2019-11-04T08:46:03Z")

</div>

Great!  
So I can say that the wording of the doc is not totally accurate…

MENTAL NOTE: If it’s false by default, just take care and don’t forget to use delegate\_facts = true (setup module) if you don’t want to override the facts.

Regarding “your example” I meant that It was suggested by you to check 🙂

Thank you very much Vlado, hope it helps for my certification exam this week!

Cheers.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 4, 2019, 9:35am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/8 "2019-11-04T09:35:30Z")

</div>

No. The wording is OK.  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_delegation.html#delegated-facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegated-facts)

&nbsp;&nbsp;Short story

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [November 4, 2019, 11:09am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/9 "2019-11-04T11:09:26Z")

</div>

I understand this the other way around:

_Short story_

---

<div class="post-metadata">

**Author:** ![Drew\_Northup](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@Drew\_Northup](https://forum.ansible.com/u/Drew_Northup)\
**Post date:** [November 4, 2019, 3:37pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/10 "2019-11-04T15:37:22Z")

</div>

Ok,  
There’s several things going on here:  
(1) The wording in the doc isn’t great and it isn’t grammatically “correct” either, so it is not “OK”;  
(2) The wording in the doc is accurate relative the originally intended function of the “delegate\_facts” switch;  
(3) The metaphor “delegate\_facts” isn’t very helpful in understanding what is actually going on—as facts aren’t what is being delegated, but it is in fact the custody of them that is.

Item #3 on my list is clearly (to a native speaker / writer of English) indicated in the paragraph following the example code on the documentation page. Setting the “delegate\_facts” switch to true means that not only are the shorthand host vars (EG: “ansible\_hostname”) of the original target host PRESERVED in the environment running on the delegate target host, but it also means that facts gathered on the delegate host end up IN THE CUSTODY OF the original target host (or host group) that will be running the play. This is done without overriding the shorthand host vars, as to do so would, within the constraining metaphor of Ansible, be without any intended meaning.

Vincente,  
Does that explanation help?

Vlado,  
Do you want to proof a quick fix to the grammar & wording of the doc page (before or after I post a PR)?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 4, 2019, 4:43pm UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/11 "2019-11-04T16:43:47Z")

</div>

Sure. Post it here. Others might be interested too.

&nbsp;&nbsp;Thank you,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Vicente\_Dominguez](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vicente\_Dominguez](https://forum.ansible.com/u/Vicente_Dominguez)\
**Post date:** [November 5, 2019, 7:51am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/12 "2019-11-05T07:51:57Z")

</div>

Thank you both! Eveything clear now 🙂

By the way, what does “PR” mean?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 5, 2019, 8:33am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/13 "2019-11-05T08:33:13Z")

</div>

It is "Pull Request".  
[https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests)

No PR by Benjamin yet  
[https://github.com/ansible/ansible/pulls](https://github.com/ansible/ansible/pulls)

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 5, 2019, 8:35am UTC](https://forum.ansible.com/t/delgate-facts-understanding/31589/14 "2019-11-05T08:35:51Z")

</div>

It is "Pull Request".  
[https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests)

No PR by Drew yet  
[https://github.com/ansible/ansible/pulls](https://github.com/ansible/ansible/pulls)

Cheers,

&nbsp;&nbsp;-vlado
