# Delegate \_execute\_method and access inventory in ActionModule

**URL:** <https://forum.ansible.com/t/delegate-execute-method-and-access-inventory-in-actionmodule/37736>\
**Category:** Ansible Project\
**Created:** [March 6, 2023, 7:15pm UTC](https://forum.ansible.com/t/delegate-execute-method-and-access-inventory-in-actionmodule/37736 "2023-03-06T19:15:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix1](https://avatars.discourse-cdn.com/v4/letter/f/3e96dc/32.png) [@Felix1](https://forum.ansible.com/u/Felix1)\
**Post date:** [March 6, 2023, 7:15pm UTC](https://forum.ansible.com/t/delegate-execute-method-and-access-inventory-in-actionmodule/37736/1 "2023-03-06T19:15:20Z")

</div>

Hi all,

I am currently developing an Action-module which should do the following:

I have a playbook like this:

- hosts: target-host  
tasks:
- my\_module:  
src\_host: source-host  
regex: some\_regex

It should be checked if a file which meets the regex can be found on the source-host. If so, it should be copied to the target-host via the controller-node. I can’t copy the file directly because of security-restrictions.

Currently I solved this by just using paramiko which works fine but isn’t a clean solution.

Questions/What I want to do instead:

1. I would like to access the inventory inside of my Action-Module to get ansible\_user, ansible\_password and ansible\_port of the source-host and target-host. How can I achieve this? I tried to use the InventoryManager without success. Currently i provide all these values as parameters.

2. A far better solution would be, to use \_execute\_module and execute the find-module on the source-host and then copy the file via the copy-module to the controller-node and the target-host. But it seems there is no delegate-option for execute\_module so (by looking at the play from above) it will always be executed on the target-host

Any help or pointing me into the right direction would be very much apricated. Couldn’t find any suitable solution so far even after hours.

Thanks and best regads,  
Felix

---

<div class="post-metadata">

**Author:** ![atul\_mohan](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@atul\_mohan](https://forum.ansible.com/u/atul_mohan)\
**Post date:** [August 25, 2023, 5:55am UTC](https://forum.ansible.com/t/delegate-execute-method-and-access-inventory-in-actionmodule/37736/2 "2023-08-25T05:55:00Z")

</div>

# Import required modules

from ansible.plugins.action import ActionBase

class ActionModule(ActionBase):  
def run(self, tmp=None, task\_vars=None):

# Initialize variables

result = super(ActionModule, self).run(tmp, task\_vars)  
should\_delegate = True # Determine if delegation is needed

# Access inventory variables

source\_host\_vars = self.\_task\_vars[‘hostvars’][self.\_task.args[‘src\_host’]]  
target\_host\_vars = self.\_task\_vars[‘hostvars’][self.\_task.args[‘inventory\_hostname’]]

# Prepare task\_args for remote task execution

task\_args = {  
‘module\_name’: ‘find’,  
‘module\_args’: f"path={source\_host\_vars[‘source\_path’]} patterns={{ {self.\_task.args[‘regex’]} }}",  
‘inventory’: self.\_task.\_inventory,  
‘subset’: [self.\_task.args[‘src\_host’]],  
}

if should\_delegate:

# Remote execution

result[‘remote\_result’] = self.\_execute\_module(task\_args=task\_args)  
else:

# Local execution

# Perform necessary local actions

result[‘local\_result’] = “Performed local actions”

# Handle file transfers

if should\_delegate:

# Execute remote copy task

copy\_task\_args = {  
‘module\_name’: ‘copy’,  
‘module\_args’: f"src={result[‘remote\_result’][‘files’][0]} dest=/destination/path",  
‘inventory’: self.\_task.\_inventory,  
‘subset’: [self.\_task.args[‘inventory\_hostname’]],  
}  
result[‘copy\_result’] = self.\_execute\_module(task\_args=copy\_task\_args)

return result
