# "Delegating" handlers to other hosts

**URL:** <https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707>\
**Category:** Ansible Project\
**Created:** [October 17, 2013, 1:02pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707 "2013-10-17T13:02:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [October 17, 2013, 1:02pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/1 "2013-10-17T13:02:28Z")

</div>

Hi all,

Is there any way to delegate handler execution on another host ?

Scenario : some client pushes part of a configuration (e.g. backup  
client) to a server and triggers configuration rebuild (assemble) on the  
server.

Of course the client in this scenario could issue assemble+delegate\_to  
but I would rather keep the whole logic contained in the right playbook,  
and rebuild only when it is required to.

Thanks,

M

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [October 17, 2013, 1:27pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/2 "2013-10-17T13:27:44Z")

</div>

Excerpts from Michel Blanc's message of 2013-10-17 09:02:28 -0400:

> Is there any way to delegate handler execution on another host ?

I think using 'delegate\_to' on the handler itself will do what you  
expect, but test it before you take my word for it.

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [October 17, 2013, 1:33pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/3 "2013-10-17T13:33:50Z")

</div>

Nice idea, I'll try it out.  
Thanks Morgan.

M

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [October 17, 2013, 8:59pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/4 "2013-10-17T20:59:23Z")

</div>

Did that work for you, Michel? I have a similar situation, but I need to copy files to and delegate handlers to a group of servers. I have something like this:

- name: Deploy  
hosts: source\_box

tasks:

- name: copy files  
command: rsync -az … {{ item }}:/dir  
with\_items: groups[‘destination\_boxes’]  
notify: service restart

handlers:

- name: service restart  
service: name=service state=restarted  
delegate\_to: SERVER

If SERVER is, say, a literal IP address, it all works. But if I have more than one destination server, I need to make SERVER a variable, and that doesn’t work. Any ideas? Am I missing something?

Nathan

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 17, 2013, 9:14pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/5 "2013-10-17T21:14:40Z")

</div>

this should work:

delegate\_to: “{{item}}”"with\_items: groups[‘loadbalancers’]  
or any list of servers

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [October 17, 2013, 9:57pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/6 "2013-10-17T21:57:50Z")

</div>

Thanks Brian, that should work well enough. What about the case where the rsync only changes files on a subset of the destination servers though? Is there a way to make sure that only the changed servers get restarted?

Nathan

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 17, 2013, 10:38pm UTC](https://forum.ansible.com/t/delegating-handlers-to-other-hosts/13707/7 "2013-10-17T22:38:08Z")

</div>

Probably! 🙂

When you are delegating you could still but a “when” condition on it.

If you need to compare facts on other hosts you can use the “hostvars[hostname][‘foo\_variable’]” method to get at them.
