# problem with expanditg variables in a template

**URL:** <https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846>\
**Category:** Ansible Project\
**Created:** [November 21, 2014, 4:39pm UTC](https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846 "2014-11-21T16:39:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean\_Philippe\_Caruan](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jean\_Philippe\_Caruan](https://forum.ansible.com/u/Jean_Philippe_Caruan)\
**Post date:** [November 21, 2014, 4:39pm UTC](https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846/1 "2014-11-21T16:39:23Z")

</div>

Hi,

I have a problem provisionning my /etc/hosts files : it seems to me that variables are not expanded, and it used to work previously.  
I am using ansible 1.7.2.

My host.j2 looks like this:  
127.0.0.1 localhost  
127.0.1.1 {{ inventory\_hostname }}  
{% if “frontservers” in group\_names or “lbservers” in group\_names %}  
{% for host in [“{{ site\_one }}”, “{{ site\_two }}”, “{{ site\_three }}”]%}  
127.0.1.1 {{ host }}  
{% endfor %}  
{% endif %}

My sever file looks like this:  
[frontservers]  
[some-server-01.xxx.com](http://some-server-01.xxx.com)  
[some-server-02.xxx.com](http://some-server-02.xxx.com)

[all:vars]  
site\_one=[someurl-01.xxx.com](http://someurl-01.xxx.com)  
site\_two=[someurl-02.xxx.com](http://someurl-02.xxx.com)  
site\_three=[someurl-03.xxx.com](http://someurl-03.xxx.com)

Expected result on [some-server-01.xxx.com](http://some-server-01.xxx.com) for instance:  
127.0.0.1 localhost  
127.0.1.1 [some-server-01.xxx.com](http://some-server-01.xxx.com)  
127.0.1.1 [someurl-01.xxx.com](http://someurl-01.xxx.com)  
127.0.1.1 [someurl-02.xxx.com](http://someurl-02.xxx.com)  
127.0.1.1 [someurl-03.xxx.com](http://someurl-03.xxx.com)

Actual result on [some-server-01.xxx.com](http://some-server-01.xxx.com):  
127.0.0.1 localhost  
127.0.1.1 [some-server-01.xxx.com](http://some-server-01.xxx.com)  
127.0.1.1 {{ site\_one }}  
127.0.1.1 {{ site\_two }}  
127.0.1.1 {{ site\_three }}

What’s wrong ?  
How come it stopped working ?

I tried using simple quotes, double quotes in my template file, but the result is the same.

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 21, 2014, 4:45pm UTC](https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846/2 "2014-11-21T16:45:05Z")

</div>

too many mustaches, you don't need them in:

{% for host in ["{{ site\_one }}", "{{ site\_two }}", "{{ site\_three }}"]%}

do it this way:

{% for host in [site\_one, site\_two, site\_three] %}

---

<div class="post-metadata">

**Author:** ![Jean\_Philippe\_Caruan](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jean\_Philippe\_Caruan](https://forum.ansible.com/u/Jean_Philippe_Caruan)\
**Post date:** [November 21, 2014, 4:49pm UTC](https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846/3 "2014-11-21T16:49:03Z")

</div>

Thanks this worked very well !

How come my previous version with "many mustaches" stopped working  
suddenly ?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 21, 2014, 5:03pm UTC](https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846/4 "2014-11-21T17:03:00Z")

</div>

I'm not sure how it worked in the first place, this is pure jinja2

---

<div class="post-metadata">

**Author:** ![Jean\_Philippe\_Caruan](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jean\_Philippe\_Caruan](https://forum.ansible.com/u/Jean_Philippe_Caruan)\
**Post date:** [November 21, 2014, 5:08pm UTC](https://forum.ansible.com/t/problem-with-expanditg-variables-in-a-template/16846/5 "2014-11-21T17:08:20Z")

</div>

> I'm not sure how it worked in the first place, this is pure jinja2

It worked very fine until recently, and I have been using this playbook  
like this for almost a year on my production servers.  
It doesn't matter anyway, since we have a working (and lighter) solution.

Thanks again.
