# Jinja2 in vars/main.yml

**URL:** <https://forum.ansible.com/t/jinja2-in-vars-main-yml/15490>\
**Category:** Ansible Project\
**Created:** [June 18, 2014, 3:05pm UTC](https://forum.ansible.com/t/jinja2-in-vars-main-yml/15490 "2014-06-18T15:05:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vasco\_Figueira](https://avatars.discourse-cdn.com/v4/letter/v/9f8e36/32.png) [@Vasco\_Figueira](https://forum.ansible.com/u/Vasco_Figueira)\
**Post date:** [June 18, 2014, 3:05pm UTC](https://forum.ansible.com/t/jinja2-in-vars-main-yml/15490/1 "2014-06-18T15:05:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 18, 2014, 4:03pm UTC](https://forum.ansible.com/t/jinja2-in-vars-main-yml/15490/2 "2014-06-18T16:03:35Z")

</div>

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.
