Passing module params conditionally

296 views
Skip to first unread message

Hagai Kariti

unread,
Sep 17, 2014, 10:14:31 AM9/17/14
to ansible...@googlegroups.com
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 :) ). How should I go about doing that in Ansible 1.7? Do I have to keep my own default values?

Thanks

James Cammarata

unread,
Sep 17, 2014, 10:41:03 AM9/17/14
to ansible...@googlegroups.com
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!

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8aa00f58-62f8-4bc4-a243-306dcaebbefc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hagai Kariti

unread,
Sep 17, 2014, 10:59:46 AM9/17/14
to ansible...@googlegroups.com

On Wednesday, September 17, 2014 5:41:03 PM UTC+3, James Cammarata wrote:
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!

David Wittman

unread,
Jul 10, 2015, 11:34:53 AM7/10/15
to ansible...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages