Q: use of delegated_facts?

1,922 views
Skip to first unread message

Sieds Pallas

unread,
Sep 15, 2017, 5:02:22 AM9/15/17
to Ansible Project
Hi,
I am getting confused on the working and purpose of delegated_facts.
If I read the manual correctly:


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
---
- name: test delegate functionality
  hosts: invhosts
  tasks:
  - name: setup
    setup:
    delegate_to: "{{ item }}"
#    delegate_facts: true
    with_items: "{{ groups['delegates'] }}"
  - name: show
    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] *******************************************************************************************************************************************
ok: [node1]
TASK [setup] *****************************************************************************************************************************************************
ok: [node1 -> node2] => (item=node2)
TASK [show] ******************************************************************************************************************************************************
ok: [node1] => {
    "changed": false,
    "msg": "from fact of node1, ansible_hostname = node2.\n"
}
PLAY RECAP *******************************************************************************************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0  

#<!--
 So with delegate_to, the facts are gotten from the delagate_to host instead of from the current managed inventory_host
#-->

$ vim testdelegate.yml
#<!-- uncomment delegate_facts -->


$ ansible-playbook testdelegate.yml
PLAY [test delegate functionality] *******************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [node1]
TASK [setup] *****************************************************************************************************************************************************
ok: [node1 -> node2] => (item=node2)
TASK [show] ******************************************************************************************************************************************************
ok: [node1] => {
    "changed": false,
    "msg": "from delegated fact of node1, ansible_hostname = node1.\n"
}
PLAY RECAP *******************************************************************************************************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0
>>>>>>>>>>>>>>>>

#<!--
 So with delegate_facts, the facts are gotten from the current managed inventory_host instead of from the delegate_to host.
#-->

Kai Stian Olstad

unread,
Sep 17, 2017, 12:09:50 PM9/17/17
to ansible...@googlegroups.com
On 15. sep. 2017 11:02, Sieds Pallas wrote:
> 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
>
> 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 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.

---
- hosts: node1
gather_facts: no
tasks:
- setup:
delegate_to: node2
#delegate_facts: true

- debug: var=hostvars['node1'].ansible_hostname
- debug: var=hostvars['node2'].ansible_hostname

Run the playbook again after you uncomment delegate_facts.

Did this help?


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages