# Action plugin: Delegate task to different host

**URL:** <https://forum.ansible.com/t/action-plugin-delegate-task-to-different-host/21916>\
**Category:** Ansible Developer\
**Created:** [July 4, 2016, 1:20pm UTC](https://forum.ansible.com/t/action-plugin-delegate-task-to-different-host/21916 "2016-07-04T13:20:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [July 4, 2016, 1:20pm UTC](https://forum.ansible.com/t/action-plugin-delegate-task-to-different-host/21916/1 "2016-07-04T13:20:47Z")

</div>

Holla!

I have an action plugin that will run some modules on the target host per _self.\_execute\_module(…)_

Now I want one single module to be delegated to a different host.\* The name of this host is a parameter of my custom module.

I have seen inside the [synchonize action plugin](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/action/synchronize.py) that you can delegate a module to a different host. In synchronize it is delegating to localhost and I can reproduce this in my plugin. But I am failing to delegate the module to a 3rd host. I was puzzling around with all the options of the _task\_vars_ which gets passed to s_elf.\_execute\_module_, created a new connection through the _connection\_loader_ and what not, but the module still is executed on the original target host.

Is this possible? Can someone point me to in the right direction?

Thanks,  
Daniel

\*Yes, it does make perfectly sense, because this is about managing a vm-host and its guests.

---

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [July 4, 2016, 1:48pm UTC](https://forum.ansible.com/t/action-plugin-delegate-task-to-different-host/21916/2 "2016-07-04T13:48:49Z")

</div>

Finally figured it out. I was missing to set the _remote\_addr_ to the correct hostname. Below snippet works, redirecting the module to _OTHER.HOST.HERE_.

`  
def run(self, tmp=None, task\_vars=None):

result = super(ActionModule, self).run(tmp, task\_vars)

self.\_task.args[“\_substitute\_controller”] = True  
self.\_task.args.pop(“use\_ssh\_args”, None)  
self.\_play\_context.delegate\_to = “OTHER.HOST.HERE”  
self.\_play\_context.remote\_addr = self.\_play\_context.delegate\_to  
self.\_play\_context.connection = “ssh”  
new\_stdin = self.\_connection.\_new\_stdin  
new\_connection = connection\_loader.get(“ssh”, self.\_play\_context, new\_stdin)  
self.\_connection = new\_connection

result.update(self.\_execute\_module(task\_vars=task\_vars, …))

return result  
`
