# module development - module.run\_command in functions

**URL:** <https://forum.ansible.com/t/module-development-module-run-command-in-functions/35209>\
**Category:** Ansible Project\
**Created:** [May 21, 2021, 2:43pm UTC](https://forum.ansible.com/t/module-development-module-run-command-in-functions/35209 "2021-05-21T14:43:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel\_Machado](https://avatars.discourse-cdn.com/v4/letter/g/e0b2c6/32.png) [@Gabriel\_Machado](https://forum.ansible.com/u/Gabriel_Machado)\
**Post date:** [May 21, 2021, 2:43pm UTC](https://forum.ansible.com/t/module-development-module-run-command-in-functions/35209/1 "2021-05-21T14:43:00Z")

</div>

Hi,

I’m developping a module for linux with several functions.  
Most of the functions i develop need to launch binary executables.  
I know, the best practice is to use module.run\_command() function to launch commands. But i instantiate the module object in the main() function. So, the module object cannot be used in the functions i develop.

What should i do :

- Pass the module as a variable to myfunctions (myfunctions(module)) ?
- Instantiate another AnsibleModule object in each function ?
- Use os.subprocess() function or equivalent in my functions ?

Why using os.subprocess() is not recommended ?

### begin - code example

myfunction1(module)  
return module.run\_command(“/usr/bin/ip route show”)[1]

def main():  
module = AnsibleModule(argument\_spec = dict())  
result = dict()  
result[‘routes’] = myfunction1(module)

return module.exit\_json(\*\*result)

### end - code example

Have a nice weekend.  
Gabriel.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 21, 2021, 2:51pm UTC](https://forum.ansible.com/t/module-development-module-run-command-in-functions/35209/2 "2021-05-21T14:51:34Z")

</div>

The typical pattern is to pass the AnsibleModule instance to your functions.

---

<div class="post-metadata">

**Author:** ![Gabriel\_Machado](https://avatars.discourse-cdn.com/v4/letter/g/e0b2c6/32.png) [@Gabriel\_Machado](https://forum.ansible.com/u/Gabriel_Machado)\
**Post date:** [May 23, 2021, 6:43pm UTC](https://forum.ansible.com/t/module-development-module-run-command-in-functions/35209/3 "2021-05-23T18:43:38Z")

</div>

Thank you very much for the reply.

I’ll do that 🙂
