Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally add additional arguments to command module with Ansible

Tags:

ansible

jinja2

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.

like image 718
user3270760 Avatar asked Mar 08 '23 21:03

user3270760


1 Answers

Use conditional:

{{ ('-r ' + last_one_optional) if last_one_optional is defined else '' }}
like image 181
Konstantin Suvorov Avatar answered May 09 '23 11:05

Konstantin Suvorov