Jinja2 in vars/main.yml

Hi,

After a task

  • name: check file existence
    stat: path=/var/run/file
    register: file

I can do this in any of my tasks

…something… my_param={% if file.stat.exists %} file {% else %} url {% endif %}

to get my_param right according to whether ‘file’ exists or not. However I would like to just write in my tasks ‘my_param={{my_var}}’.

For that I would like to define a variable my_var in vars/main.yml, but I can’t use the same jinja2-fu there. It complains as soon as it finds a ‘%’ (different execution context?)

How does one evaluate vars with this kind of logic constructs?

Thanks in advance.

Best regards,

Vasco

Definitely cleaner to keep that in variables files.

You can definitely do this:

my_var: “{% if foo %}x{% else %}y{% endif %}”

It’s not too pretty either though, usually ansible conditionals end up looking cleaner.