# Setting conditional for template

**URL:** <https://forum.ansible.com/t/setting-conditional-for-template/12134>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 11, 2012, 3:44pm UTC](https://forum.ansible.com/t/setting-conditional-for-template/12134 "2012-12-11T15:44:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [December 11, 2012, 3:44pm UTC](https://forum.ansible.com/t/setting-conditional-for-template/12134/1 "2012-12-11T15:44:39Z")

</div>

In a playbook I would like to set a conditional variable for use in a template, e.g.

{% if is\_master %}  
…  
{% endif %}

But if I follow the recipe at  
[http://ansible.cc/docs/playbooks2.html#conditional-execution](http://ansible.cc/docs/playbooks2.html#conditional-execution)

e.g.

vars:  
is\_master: “‘$ansible\_hostname’ == ‘foo’”

Then I find that {{ is\_master }} expands to the literal string ‘$ansible\_hostname’ == ‘foo’

Can I get ansible to evaluate the condition before passing it to the template?

I’m using ansible 0.7.2 from the ubuntu 12.04 repos.

For now my workaround is to put the logic in the template:

{% if ansible\_hostname == ‘foo’ %}  
{% set is\_master = true %}  
{% endif %}

Thanks,

Brian.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [December 12, 2012, 12:11am UTC](https://forum.ansible.com/t/setting-conditional-for-template/12134/2 "2012-12-12T00:11:04Z")

</div>

that example is not for what you are doing, it's for saving  
expressions for reuse in only\_if

you should include the full if test in your template
