# set\_fact variable not working in template

**URL:** <https://forum.ansible.com/t/set-fact-variable-not-working-in-template/24387>\
**Category:** Ansible Project\
**Created:** [May 3, 2017, 11:56am UTC](https://forum.ansible.com/t/set-fact-variable-not-working-in-template/24387 "2017-05-03T11:56:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiran\_sangeetam](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kiran\_sangeetam](https://forum.ansible.com/u/kiran_sangeetam)\
**Post date:** [May 3, 2017, 11:56am UTC](https://forum.ansible.com/t/set-fact-variable-not-working-in-template/24387/1 "2017-05-03T11:56:44Z")

</div>

Hi Everyone,

I want to use variable that I defined in set\_fact task in template. But it’s not working. Can anyone please suggest if I’m doing anything wrong.

Below is the syntax I am using in tasks file.

- name: Setting fact for logfile  
set\_fact: log\_file\_path=“/home/log.txt” include\_path=“true”  
when: “‘job’ in inventory\_hostname”

and below is the syntax I’m using in template.

{%if include\_path == “true” %}  
some steps  
{% else %}  
some steps  
{%endif%}

Even include\_path=“true” while executing the playbook, template is always adding the steps in else statement.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 4, 2017, 7:04am UTC](https://forum.ansible.com/t/set-fact-variable-not-working-in-template/24387/2 "2017-05-04T07:04:19Z")

</div>

Since you haven't explained what you are trying to do some guessing is required.

Your when will only trigger if the inventory\_hostname is "job".  
So for all the other hosts this fact is not set.

The in clause is normally used to find an element in a dictonary, inventory\_hostname is a string.

I guess you are looking for .find as shown in this example  
[https://docs.ansible.com/ansible/playbooks\_conditionals.html#register-variables](https://docs.ansible.com/ansible/playbooks_conditionals.html#register-variables)

---

<div class="post-metadata">

**Author:** ![kiran\_sangeetam](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kiran\_sangeetam](https://forum.ansible.com/u/kiran_sangeetam)\
**Post date:** [May 5, 2017, 5:46pm UTC](https://forum.ansible.com/t/set-fact-variable-not-working-in-template/24387/3 "2017-05-05T17:46:26Z")

</div>

Thanks for the reply.  
My bad, I did not explain it clearly.

Actually what I’m trying to achieve is:

I want to give conditions on template that I am copying to server. When the client hostname has ‘job’ word in it then template should have some text else template should have some other text.

For example: if host name is ‘[dev-jobs.company.com](http://dev-jobs.company.com)’, the template I am copying should have log\_file\_path=“/home/log.txt” otherwise it should have log\_file\_path=“/tmp/log.txt”

What is the best way to achieve this ??

---

<div class="post-metadata">

**Author:** ![Larry\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@Larry\_Smith1](https://forum.ansible.com/u/Larry_Smith1)\
**Post date:** [May 5, 2017, 9:51pm UTC](https://forum.ansible.com/t/set-fact-variable-not-working-in-template/24387/4 "2017-05-05T21:51:45Z")

</div>

Have you tried with your conditional in your template as:

{% if include\_path %}  
some steps  
{% else %}  
some steps  
{% endif %}

Or add a debug prior to template for:

- debug: var=include\_path

My guess would be you will see include\_path as True rather than true.
