Passing module params conditionally

Hi
I have a usecase in which I want to pass some parameters to a module only if I have a variable defined with value for them. If I don’t, I want the module to use its default value for these parameters. In ansible 1.5.5 (I know, old as hell), I could do this:

module: {% if value is defined %}param={{value}}{% endif %}

However later versions of ansible seem to have removed it due to security concerns (which I don’t understand, but that’s a different topic :slight_smile: ). How should I go about doing that in Ansible 1.7? Do I have to keep my own default values?

Thanks

Hi Hagai,

We’ve recently added the ability to omit parameters if they are not defined via the “| default(omit)” filter. The documentation regarding this is here: http://docs.ansible.com/playbooks_variables.html#omitting-undefined-variables-and-parameters. This will be included in 1.8, so for now with 1.7 you will still have to default the parameter to some value if it is not defined.

The reason we now disallow what you’re doing in your example is the fact that someone could try to inject additional parameters into the module arguments via variables, so Ansible now expects a consistent number of parameters both before and after the templating of module params is done.

Let us know if you have any further questions regarding this.

Thanks!

Hi Hagai,

We’ve recently added the ability to omit parameters if they are not defined via the “| default(omit)” filter. The documentation regarding this is here: http://docs.ansible.com/playbooks_variables.html#omitting-undefined-variables-and-parameters. This will be included in 1.8, so for now with 1.7 you will still have to default the parameter to some value if it is not defined.

Ooh, that’s cool.

The reason we now disallow what you’re doing in your example is the fact that someone could try to inject additional parameters into the module arguments via variables, so Ansible now expects a consistent number of parameters both before and after the templating of module params is done.

I guess I’m not that afraid of weird variable values. Seems fair to enforce this, though.

Thanks a lot!

The documentation for this feature has been moved to https://docs.ansible.com/playbooks_filters.html#omitting-undefined-variables-and-parameters.