I want to conditionally define a variable in an Ansible playbook like this:
my_var: \"{{ \'foo\' if my_condition}}\"
You could use something like this:
my_var: "{{ 'foo' if my_condition else '' }}"
The 'else' will happen if condition not match, and in this case will set a empty value for the variable. I think this is a short, readable and elegant solution.