Ansible Collection | dependencies between module options

I the maintainer of netscaler.adc 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 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.

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.)

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:

    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))
1 Like

thank you @felixfontein. It worked.

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