Get only playbook params in an ansible module

I am writing netscaler.adc collection for NetScaler devices.

After instantiating AnsibleModule class, we get a module object.
In the module object, there is a params member – module.params

from ansible.module_utils.basic import AnsibleModule
module = AnsibleModule()
print(module.params)

module.params has all the parameters for the resource. For example, if a resource has four attributes – a, b, c, d, and if the user provides only two attributes a and b, I am getting all the four attributes (c and d with their default values.
I do not want to get the non-playbook attributes.

How to achieve this.

My code is below – So far, I was of the assumption that module.params has only the playbook-attributes.

Can someone please help me a way to get only playbook-attributes?

Thanks a ton in advance.

Hello @sumanth-lingappa,

Not that I’ve developed many collection modules myself, but on the ones I did (pretty simple ones actually) I use to get the arguments independently as dict elements instead of getting the whole params dict. For example, if I have these arguments;

def run_module():
    module_args = dict(
        provider=dict(
            host=dict(type="str", required=True),
            user=dict(type="str", required=True, no_log=True),
            password=dict(type="str", required=True, no_log=True),
        ),
        param1=dict(type="str", required=False),
        param2=dict(type="str", required=False),
    )

Then I can get only the ones I want, e.g. like this:

 print(module.params["provider"])

Is that what you were looking for? Also, Notice that you can add the required=False argument to any module_args param instead of assigning a default value.

Cheers,

1 Like

The solution I found was NOT to use default args as these default args will go with the POST method even if they have not been provided in the playbook.

1 Like

That’s neat @sumanth-lingappa I’m glad you came up with a solution yourself :slight_smile:

Just for housekeeping sake, may I ask you to tick the solved check on the post you believe helped you the most? (of course, you can set your own post as the solution).

Cheers and happy coding!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.