Multiple module_args

Hi,

I am developing a module (python) which I am calling using ansible.runner and I need to so with multiple module args.

For example:

args1 = str(sys.argv[0])

args2 = str(sys.argv[1])
args3 = str(sys.argv[2])

From what I can tell module_arg in all my noobness, will only pass a single string quoted string?
How would I pass multiple arguments?

Thanks,
Luke

Except for a few very special ansible modules like command, module args should be key= value. From the playbook’s point of view this can be specified as a yaml dict or a single string of key= value paid separated by spaces.

Inside of the controller, ansible takes either of these forms and transforms them into a python dictionary. For new style ansible modules (those written in python and using the helper class in lib/ansible/module_utils/basic.py ) this dict is turned into a string representation and embedded in the module. Then the module is passed over the wire and executed on the remote machine.

On the remote machine, the code inside module_utils/basic.py takes care of turning the the args back into a python dict from the string representation so that the rest of the code in the middle can access the parameters via that dict.

The easiest module I know of to look at to get the hang of this is ping: https://github.com/ansible/ansible-modules-core/blob/devel/system/ping.py

It only takes a single parameter (data)
But you can add more by making new entries in the argument_spec as you experiment.

-Toshio

Thanks Toshio!

So I could effectly do something like the following:

module = AnsibleModule(
argument_spec = dict(
arg1=dict(required=True, default=None),
arg2=dict(required=False, default=None),
arg3=dict(required=False, default=None),
),
supports_check_mode = True
)

Where I have a lack of understanding, is how I would pass those values/args over in ansible.runner:

runner = ansible.runner.Runner(
module_name=‘testmodule’,
module_args=‘arg1 arg2 arg3’

)

or

runner = ansible.runner.Runner(
module_name=‘testmodule’,
module_args= [‘arg1’,arg2’,arg3’]

)

Quick update. Its working now…

module_args=‘arg1=hello arg2=world’

In the key=pair, how can I use variables? For example, like this?

def create_repo_name(repo_name, desc, baseurl, enabled): mod = ansible.runner.Runner( module_name = 'yum_repository' module_args = 'name=repo_name description=desc baseurl=baseurl enabled=enabled' )