# Get only playbook params in an ansible module

**URL:** <https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877>\
**Category:** Get Help\
**Tags:** module, playbook, collections\
**Created:** [December 13, 2023, 2:13am UTC](https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877 "2023-12-13T02:13:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumanth-lingappa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sumanth-lingappa/32/839_2.png) [@sumanth-lingappa](https://forum.ansible.com/u/sumanth-lingappa)\
**Post date:** [December 13, 2023, 2:13am UTC](https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877/1 "2023-12-13T02:13:41Z")

</div>

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`

```python
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.

> <https://github.com/netscaler/ansible-collection-netscaleradc/blob/6912cf34cc1b2026ad8261ae1fd25f88a2ed4d67/plugins/module_utils/module_executor.py#L175>

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

Thanks a ton in advance.

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [December 13, 2023, 2:22pm UTC](https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877/2 "2023-12-13T14:22:22Z")

</div>

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;

```python
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:

```python
 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,

---

<div class="post-metadata">

**Author:** ![sumanth-lingappa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sumanth-lingappa/32/839_2.png) [@sumanth-lingappa](https://forum.ansible.com/u/sumanth-lingappa)\
**Post date:** [December 14, 2023, 4:50am UTC](https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877/3 "2023-12-14T04:50:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [December 14, 2023, 12:14pm UTC](https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877/4 "2023-12-14T12:14:55Z")

</div>

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

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 13, 2024, 12:15pm UTC](https://forum.ansible.com/t/get-only-playbook-params-in-an-ansible-module/2877/5 "2024-01-13T12:15:34Z")

</div>

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