Passing parameters to custom module

Hi,

I have written custom modules in python.
I am calling the modules from my task file like below:

  • name: Collect logstash details
    action: get_logstash_details path={{ logstash_path }}
    register: result
  • name: Display the logstash details
    debug: var=result

get_logstash_details is my custom module.
path is the parameter i want to pass to the custom module.

This paramter is accessed in custom module as mentioned below
path=sys.argv[1]

Queries:

  1. Is that the right way to send parameters to custom module?

I am getting the below error:
fatal: [172.19.10.31]: FAILED! => {“changed”: false, “failed”: true, “invocation”: {“module_args”: {“path”: “/opt/logstash_standalone/logstash-2.1.1/bin/logstash”}, “module_name”: “get_logstash_details”}, “msg”: “unsupported parameter for module: path”}

Thanks in advance,
Mona G

Hi Mona,

please first import the below line in your module:
from ansible.module_utils.basic import *

After importing it use below step to get value of variable:

module = AnsibleModule(argument_spec = dict( logstash_path = dict(required=True, aliases=['logstash_path ']))
logstash_path = module.params.get(‘logstash_path’)

regards
Prakul

You can also return the information you need as Ansible facts, which means you can just reference them as normal Ansible variables and you don’t have to use ‘register’.

An example of a simple custom module I wrote is https://github.com/pmarkham/ansible-ipcalc/blob/master/ipcalc