module development - module.run_command in functions

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.

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

Thank you very much for the reply.

I’ll do that :slightly_smiling_face: