I have a task in an ansible role which calls a script with a bunch of args that are defined as ansible variables. The task looks like this:
- name: Generate config files
command: /etc/whatever/gen-config.sh -n {{domain}} -m {{ another_option }} -w {{ws_enabled | default('N') }} -r {{ last_one_optional}}
The problem is that the -r is an optional argument, so sometimes I don't have an ansible variable to pass to it, and the script complained if I used a default("") instead. Is there some way to optionally include the -r only if {{last_one_optional}} is defined? I have seen some of the jinja inline if statements and I didn't know if that would work for ansible or not.
Use conditional:
{{ ('-r ' + last_one_optional) if last_one_optional is defined else '' }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With