blanket-apply delegate_to to roles

748 views
Skip to first unread message

Dmitry Makovey

unread,
Oct 3, 2014, 1:12:46 PM10/3/14
to ansible...@googlegroups.com
I've got a case where I'd like to delegate contents of entire role over to another box. Adding "delegate_to" to every task seems strenuous and not reusable since I may use this role without delegation.

My attempt at simulating what is done with { role: X, tags: A } did not succeed:

$ cat roles/delegated_role/tasks/main.yml   
---

- name: Simple echo
  shell: hostname
  # delegate_to: "{{ central_server }}"

$ cat test_delegate.yml 
---

- hosts: all
  roles:
  - { role: delegated_role, delegate_to: "{{ central_server }}" }

yet invocation with 

$ ansible-playbook -vvv -u root -e 'central_server=foo.bar' -i inventory test_delegate.yml

yields results obtained from the server mentioned and not from 'delegated_to'. Obviously uncommenting "delegate_to" line within a role yields desired results but that means I can't reuse and it's a copy-paste exercise to transfer a role into a delegated role.

Am I doing it the wrong way? Am I applying a wrong pattern here or is there a different way to blanket-apply things like that?

Michael DeHaan

unread,
Oct 5, 2014, 2:02:21 PM10/5/14
to ansible...@googlegroups.com
Sounds like you should just configure that one server directly and missed the part of Ansible that allows managing specific host groups.

- hosts: name_of_server
  roles:
    - ...

What you have above, say if you had 500 hosts, would run each step 500 times and likely overwhelm that server.



--
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/d7b32c05-6ab4-4c98-a7b0-54779631371e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dmitry Makovey

unread,
Oct 7, 2014, 10:35:48 AM10/7/14
to ansible...@googlegroups.com


On Sunday, October 5, 2014 12:02:21 PM UTC-6, Michael DeHaan wrote:
What you have above, say if you had 500 hosts, would run each step 500 times and likely overwhelm that server.


actually that is the intent. My example was "simplified" so it's easy to understand mechanics of it, but obviously it's hard to explain reasoning for doing it so. In other words instead of "hostname" there's a command that does some "central registration" of each host, and it has to run from a central place. However in certain cases I can bypass that. 

if I'm reading your comment correctly you propose to do:

- hosts: central_server
  tasks:
  - name: register other hosts
    shell: echo "{{ item }}" >> /tmp/registry
    with_items: groups.X
 
however that disrupts the flow of the playbook and make my loops bit more complicated (for example where originally I'd have "with_items" loop iterating over items to be applied to the host I now have to implement more complicated constructs like "with_nested" etc.). 

Also moving it out like that makes role not self-sufficient - i.e. if I want to recycle my role, I have to use the role that iterates over servers in a specified group or all. 

Koteswara Rao Sakhamuri

unread,
Jun 26, 2017, 10:16:58 PM6/26/17
to Ansible Project
Hi Dmitry Makovey, 
Im also looking for the same solution, To run the play on other machine in the same task like this...

- hosts: webservers
  roles:
  - { role: iinstall_web}
  - { role: delegated_role, delegate_to: "{{ central_server }}" }

I have tried different options but failed. did you figured out how we can do this?
Reply all
Reply to author
Forward
0 new messages