Action plugin: Delegate task to different host

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 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 self._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.

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
`