Gather facts with delegate_to

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

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).

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

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

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

Antoine Rouaze

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.

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

Thanks for the help !

Antoine Rouaze

The idiomatic way is:

  • hosts: thisserver
    tasks:

  • hosts: webservers
    tasks: