Gather facts with delegate_to

1,622 views
Skip to first unread message

Antoine Rouaze

unread,
Dec 18, 2014, 3:55:51 AM12/18/14
to ansible...@googlegroups.com
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

Brian Coca

unread,
Dec 18, 2014, 9:59:26 AM12/18/14
to ansible...@googlegroups.com
> 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).

--
Brian Coca

Antoine Rouaze

unread,
Dec 18, 2014, 10:06:16 AM12/18/14
to ansible...@googlegroups.com
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

Brian Coca

unread,
Dec 18, 2014, 10:22:04 AM12/18/14
to ansible...@googlegroups.com
delegate_to just executes the current task on a different target, the
returns of any delegated task is still the 'current host'.



--
Brian Coca

Antoine Rouaze

unread,
Dec 18, 2014, 10:28:45 AM12/18/14
to ansible...@googlegroups.com
Ok, so do you know any other way to retrieve facts from host not included in the play ? 

Antoine Rouaze

Brian Coca

unread,
Dec 18, 2014, 10:33:37 AM12/18/14
to ansible...@googlegroups.com
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.



--
Brian Coca

Antoine Rouaze

unread,
Dec 18, 2014, 10:44:49 AM12/18/14
to ansible...@googlegroups.com
Ok, with these two ways I can get the facts like this "hostvars['mrk-test-sql1']"

Thanks for the help !

Antoine Rouaze

Michael DeHaan

unread,
Dec 18, 2014, 12:05:11 PM12/18/14
to ansible...@googlegroups.com
The idiomatic way is:

- hosts: thisserver
  tasks: []

- hosts: webservers
  tasks:
    -

Rather than have the delegate_to in there, as that will run once per host in the host loop and will be very abusive of that extra server or servers

Or just

- hosts: all
  tasks: []

- hosts: webservers
  gather_facts: no  # already done
  tasks:
    -


--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b3a78178-b931-4300-817d-6c6d08a13f89%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages