# Assigning variable from jinja template evaluation

**URL:** <https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173>\
**Category:** Ansible Project\
**Created:** [July 30, 2016, 3:09pm UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173 "2016-07-30T15:09:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [July 30, 2016, 3:09pm UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/1 "2016-07-30T15:09:51Z")

</div>

I am hoping someone would be able to point out what I’m doing wrong here-

I have a jinja template:  
{% if subnet == ‘subnet-ab1’ %}  
subnet1  
{% elif subnet == "subnetab2’ %}  
subnet2  
{% elif subnet == "subnetab3’ %}  
subnet3  
{% elif subnet == "subnetab4’ %}  
subnet4  
{% else %}  
subnet5  
{% endif %}

I am assigning the variable in the main playbook like this:

group\_id: “{{ lookup(‘template’, 'sec\_grp.j2”) }}"

But I am getting this error:

`template error while templating string: expected token 'end of statement block', got 'subnet'`

The thing is, the below works:  
{% if subnet == ‘subnet-ab1’ %}  
subnet1  
{% endif %}

but I need more to add more lines.

Thanks.

---

<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:** [July 30, 2016, 3:33pm UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/2 "2016-07-30T15:33:12Z")

</div>

> I am hoping someone would be able to point out what I'm doing wrong here-
> 
> I have a jinja template:  
> {% if subnet == 'subnet-ab1' %}  
> subnet1  
> {% elif subnet == "subnetab2' %}  
> subnet2  
> {% elif subnet == "subnetab3' %}  
> subnet3  
> {% elif subnet == "subnetab4' %}  
> subnet4  
> {% else %}  
> subnet5  
> {% endif %}

Your are mixing single and double quotes.

> I am assigning the variable in the main playbook like this:
> 
> group\_id: "{{ lookup('template', 'sec\_grp.j2") }}"

Ditto.

---

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [July 30, 2016, 3:41pm UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/3 "2016-07-30T15:41:40Z")

</div>

I am sorry, those were typos:  
template:  
{% if subnet == ‘subnet-ab1’ %}  
subnet1  
{% elif subnet == ‘subnetab2’ %}  
subnet2  
{% elif subnet == ‘subnetab3’ %}  
subnet3  
{% elif subnet == ‘subnetab4’ %}  
subnet4  
{% else %}  
subnet5  
{% endif %}

playbook:  
group\_id: “{{ lookup(‘template’, ‘sec\_grp.j2’) }}”

---

<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:** [July 30, 2016, 4:21pm UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/4 "2016-07-30T16:21:37Z")

</div>

Testet the code in v2.1.1 and it's working for me.

---

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [July 30, 2016, 9:52pm UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/5 "2016-07-30T21:52:09Z")

</div>

Thank you for the quick response. I think I got the error to go away but I m facing a different issue now; I am using debug/msg to print the value of the variable and for some reason a newline character is being appended, for example, instead of just ‘subnet1’, I get ‘subnet1\n’. Adding ‘#jinja2:trim\_blocks: True, lstrip\_blocks: True’ doesn’t help.

---

<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:** [July 31, 2016, 7:23am UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/6 "2016-07-31T07:23:26Z")

</div>

Most likely a bug, I haven't seen this problem with the template module.

As a workaround you can use the trim filter in Ansible.  
group\_id: "{{ lookup('template', 'sec\_grp.j2') | trim }}"

---

<div class="post-metadata">

**Author:** ![a\_akin](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@a\_akin](https://forum.ansible.com/u/a_akin)\
**Post date:** [August 1, 2016, 1:12am UTC](https://forum.ansible.com/t/assigning-variable-from-jinja-template-evaluation/22173/7 "2016-08-01T01:12:28Z")

</div>

Thanks, the trim filter worked.
