I want to conditionally define a variable in an Ansible playbook like this:
my_var: \"{{ \'foo\' if my_condition}}\"
I believe you're after the default(omit)
filter. (Reference).
As per the example, mode
will behave like it wasn't set at all for the first two items in the loop.
- name: touch files with an optional mode
file:
dest: "{{item.path}}"
state: touch
mode: "{{item.mode|default(omit)}}"
loop:
- path: /tmp/foo
- path: /tmp/bar
- path: /tmp/baz
mode: "0444"