In Ansible Python module, how to differentiate between options passed as argument and options with default value?

59 Views Asked by At

How can you differentiate between a non-required module parameter that's passed to an Ansible Python module with a null value, and a non-required module parameter that's not given in the invocation?

E.g. a module person with the following module_args:

module_args = dict(
    ### Required parameters
    email = dict(type='str', default=None, required=True),
    state = dict(type='str', default=None, required=True, choices=['absent','present']),

    ### Optional parameters
    phoneNumber = dict(type='str', required=False, default=None),
    mobileNumber = dict(type='str', required=False, default=None),
)

If I don't pass the phoneNumber value in the invocation, I don't want to change it, but if I explicitly pass phoneNumber: null in the invocation, I want to clear it.

But in both cases, the invocation shows that the value passed is null.
Is there some way to determine whether a module.params['param'] has value None in the module because it was explicitly given as None versus it has None because that's the default value when the parameter is omitted?

In other words:

- name: Delete phone, leave mobile
  person:
    email: [email protected]
    state: present
    phoneNumber: null

In this case, the phoneNumber should be deleted, and the mobileNumber should be left alone. But the invocation shows that both have null as value.

I currently have solved this by setting the default value not to None but to some random string, but that feels like a really ugly solution.

0

There are 0 best solutions below