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'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]
*******************************************************************************************************************************************

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

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ac33cbe7-9ade-e2f1-0cc1-b30538631a5a%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to