module development - module.run_command in functions

22 views
Skip to first unread message

Gabriel Machado

unread,
May 21, 2021, 10:43:00 AM5/21/21
to Ansible Project
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.

Matt Martz

unread,
May 21, 2021, 10:52:25 AM5/21/21
to ansible...@googlegroups.com
The typical pattern is to pass the AnsibleModule instance to your functions.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fbcc904e-5269-4d20-a0ba-09a244889df4n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Gabriel Machado

unread,
May 23, 2021, 2:44:00 PM5/23/21
to ansible...@googlegroups.com
Thank you very much for the reply.

I'll do that 🙂

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/vqDIfUcD71w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAD8N0v8Mdo0QeSDXXdHSUJexO7t0GYuXTwiO0R7fkhT5sNDhBg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages