# Ansible Collection | dependencies between module options

**URL:** <https://forum.ansible.com/t/ansible-collection-dependencies-between-module-options/5039>\
**Category:** Get Help\
**Tags:** netscaler, module, collections\
**Created:** [April 19, 2024, 8:33am UTC](https://forum.ansible.com/t/ansible-collection-dependencies-between-module-options/5039 "2024-04-19T08:33:57Z")\
**Posts on this page:** 4\
**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:** [April 19, 2024, 8:33am UTC](https://forum.ansible.com/t/ansible-collection-dependencies-between-module-options/5039/1 "2024-04-19T08:33:57Z")

</div>

I the maintainer of [`netscaler.adc`](https://github.com/netscaler/ansible-collection-netscaleradc) ansible collection.  
I am writing a module whose options have some dependency.

Let us take options – proxy, username, password, token

Only if `proxy` is `true`, `username`, `password`, `token` is valid.  
If `username`, `password`, `token` is given, and `proxy` is not given (defaults to `false`, then Ansible should error out.

Using the [Dependencies between module options](https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html#dependencies-between-module-options) in the Ansible documentation, I could not find a way to achieve this.

However, the contrary I could achieve, that is, if `proxy` is present, and when `username`, `password`, `token` are not provided, I made ansible to error out.

```python
required_if=[
                ("proxy", True, ("username", "password", "token",), True),
            ],

```

Can anyone help me to achieve the above one (Only if `proxy` is `true`, `username`, `password`, `token` is valid.  
If `username`, `password`, `token` is given, and `proxy` is not given (defaults to `false`, then Ansible should error out.)

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [April 19, 2024, 11:36am UTC](https://forum.ansible.com/t/ansible-collection-dependencies-between-module-options/5039/2 "2024-04-19T11:36:15Z")

</div>

You have to write explicit code that does this. There’s no way to achieve this with the argument spec options that `AnsibleModule` offers.

So something like:

```py
    if not module.params['proxy']:
        for k in ('username', 'password', 'token'):
            if module.params[k] is not None:
                module.fail_json('You can only use {k} if proxy=true'.format(k=k))

```

---

<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:** [April 22, 2024, 5:28am UTC](https://forum.ansible.com/t/ansible-collection-dependencies-between-module-options/5039/3 "2024-04-22T05:28:22Z")

</div>

thank you @felixfontein. It worked.

---

<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:** [May 22, 2024, 5:29am UTC](https://forum.ansible.com/t/ansible-collection-dependencies-between-module-options/5039/4 "2024-05-22T05:29:00Z")

</div>

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