Add conditional logic to variable files?

19 views
Skip to first unread message

Robert Flaugher

unread,
Dec 12, 2017, 5:20:11 PM12/12/17
to Ansible Project
I've been using Ansible Galaxy roles from Jeff Geerling.  In his Github READMEs he seems to say that you can override variable settings in his default/main.yml and vars/*.yml files by reproducing the desired settings in a <project>/vars/main.yml file and then re-assigning them your desire values.  This works well but there are times when I'd like to use the following logic in that file:

# vars/main.yml
enable_setting
: true
...


if enable_setting
   foo
: bar
else
   baz
: barf


Is this possible?  I didn't see this mentioned in the Ansible docs on Variables and Conditionals.

Thanks.

Brian Coca

unread,
Dec 13, 2017, 10:30:03 PM12/13/17
to Ansible Project
data files have no logic, but you can use a templated value instead:

foo: "{{ enabled_setting|ternary('bar', '') }}"
baz: "{{ enabled_setting|ternary('', 'barf') }}"





--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages