problem with expanditg variables in a template

18 views
Skip to first unread message

Jean Philippe Caruana

unread,
Nov 21, 2014, 11:39:24 AM11/21/14
to ansible...@googlegroups.com
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
some-server-02.xxx.com

[all:vars]
site_one=someurl-01.xxx.com
site_two=someurl-02.xxx.com
site_three=someurl-03.xxx.com


Expected result on some-server-01.xxx.com for instance:
127.0.0.1       localhost
127.0.1.1       some-server-01.xxx.com
127.0.1.1       someurl-01.xxx.com
127.0.1.1       someurl-02.xxx.com
127.0.1.1       someurl-03.xxx.com


Actual result on some-server-01.xxx.com:
127.0.0.1       localhost
127.0.1.1       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

Brian Coca

unread,
Nov 21, 2014, 11:45:10 AM11/21/14
to ansible...@googlegroups.com
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] %}


--
Brian Coca

Jean-Philippe Caruana

unread,
Nov 21, 2014, 11:49:15 AM11/21/14
to ansible...@googlegroups.com
Thanks this worked very well !

How come my previous version with "many mustaches" stopped working
suddenly ?
Jean-Philippe Caruana
http://www.barreverte.fr

Brian Coca

unread,
Nov 21, 2014, 12:03:04 PM11/21/14
to ansible...@googlegroups.com
I'm not sure how it worked in the first place, this is pure jinja2


--
Brian Coca

Jean-Philippe Caruana

unread,
Nov 21, 2014, 12:08:29 PM11/21/14
to ansible...@googlegroups.com

> 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.
Reply all
Reply to author
Forward
0 new messages