Two sets of arguments/parameters for one module?

21 views
Skip to first unread message

Rory G

unread,
Jul 10, 2020, 12:56:15 AM7/10/20
to Ansible Development
Hi all,

I'm new to Ansible and a writing a few python network modules. I was wondering if it is possible to write a module that uses one of two different sets of parameters based on a version parameter. For example, I have the two tasks with slightly different parameters that do the same thing. I would like to call the same module for both.

Tasks Pseudocode:

- name: Task Version 1
  test_task:
    version: 1
    parameter1: test

- name: Task Version 2
  test_task:
    version: 2
    param_container:
      parameter1: test

So here the task does the same thing, but the arguments change slightly based on the version. The version must be passed in by the user somehow. Is it possible to write a module that uses different parameters based on another parameter? 

I have already tried creating multiple Ansible module objects and creating two separate modules with an abstraction layer, but nothing seems to be working.

Sorry if this is obvious, but I would really appreciate the help!

Magnus Lubeck

unread,
Jul 10, 2020, 1:30:56 AM7/10/20
to Rory G, Ansible Development
I see no problem with doing that.

Just make sure that your module has default values for both parameter1 and param_container, so that you can enter your method.

Then, once you are in your code, you can check for the version and take proper action.

//magnus

Sent from my iPhone

On 10 Jul 2020, at 06:56, Rory G <roryg...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/0d3cf09e-2955-4a89-ac6f-4f03d000ca80o%40googlegroups.com.

Rory G

unread,
Jul 10, 2020, 12:11:54 PM7/10/20
to Ansible Development
That is helpful, but would it be possible to have two sets of argument_spec for more complicated differences in the parameters? 

example pseudocode for module:

argument_spec1=dict(...)
argument_spec2=dict(...)

# get version from YAML

# pass corresponding argument_spec to AnsibleModule based on version


It seems like it's not possible with the standard module, but it would be helpful if there was a way.



On 10 Jul 2020, at 06:56, Rory G <roryg...@gmail.com> wrote:


Hi all,

I'm new to Ansible and a writing a few python network modules. I was wondering if it is possible to write a module that uses one of two different sets of parameters based on a version parameter. For example, I have the two tasks with slightly different parameters that do the same thing. I would like to call the same module for both.

Tasks Pseudocode:

- name: Task Version 1
  test_task:
    version: 1
    parameter1: test

- name: Task Version 2
  test_task:
    version: 2
    param_container:
      parameter1: test

So here the task does the same thing, but the arguments change slightly based on the version. The version must be passed in by the user somehow. Is it possible to write a module that uses different parameters based on another parameter? 

I have already tried creating multiple Ansible module objects and creating two separate modules with an abstraction layer, but nothing seems to be working.

Sorry if this is obvious, but I would really appreciate the help!

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansibl...@googlegroups.com.

Felix Fontein

unread,
Jul 10, 2020, 12:25:50 PM7/10/20
to ansibl...@googlegroups.com
Hi,

why not simply have two different modules? (With shared code in module_utils?)

You could also not use AnsibleModule, but do all the communication by
yourself (like binary modules). That way you can essentially do what you want.

Cheers,
Felix

Rory G

unread,
Jul 13, 2020, 3:21:03 PM7/13/20
to Ansible Development
Thank you, that should set me on the right track!
Reply all
Reply to author
Forward
0 new messages