Ansible Jinja Templating For issue - Help :)

27 views
Skip to first unread message

Owen Corcoran

unread,
May 23, 2017, 2:30:42 PM5/23/17
to Ansible Project
Hi Guys,

Im trying to get my ansible script to use the below for loop to populate a configfile template generated from Jinja2.

The line i cant get working is

{% for hostname in {{ redishosts }} %}
server "{{ hostname }}" {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }} check port 6379 inter 1s\r\n
{% endfor %}


Im trying to get the hostname the ip4 address and the text around it. Everytime it runs it fails stating expecting : not }

Ive tried everything i can think of but i cant get it working. Any help apreciated


Kai Stian Olstad

unread,
May 23, 2017, 2:43:23 PM5/23/17
to ansible...@googlegroups.com
On 23. mai 2017 20:30, Owen Corcoran wrote:
> Hi Guys,
>
> Im trying to get my ansible script to use the below for loop to populate a
> configfile template generated from Jinja2.
>
> The line i cant get working is
>
> {% for hostname in {{ redishosts }} %}

You can't use {{ and }} in between {% %}, so just remove them.


> server "{{ hostname }}" {{
> hostvars[host]['ansible_eth0']['ipv4']['address'] }} check port 6379 inter

I guess it's hostname and not host.


--
Kai Stian Olstad

Owen Corcoran

unread,
May 23, 2017, 6:09:31 PM5/23/17
to Ansible Project
Thanks that fixed it . Can you tell me why you can't use them . Just so I know for future reference.

Thanks

Kai Stian Olstad

unread,
May 25, 2017, 3:45:48 PM5/25/17
to ansible...@googlegroups.com
On 24. mai 2017 00:09, Owen Corcoran wrote:
> Thanks that fixed it . Can you tell me why you can't use them . Just so I know for future reference.

When you are inside {% %} block, you are in Jinja mode, and in Jinja
variables is used only by it's name.

When you are outside of {% %} you need {{ }} to say that this is a Jinja
expression.

For more information about Jinja the documentation is here
http://jinja.pocoo.org/docs/dev/templates/

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages