blanket-apply delegate_to to roles

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

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.

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.

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?