Hi,
> In my jinja2 template-file, I have the below:
>
> {% for k in client_groups %}
> {% if client_destinations[k][0].var is defined %}
> {% for i in range(0,client_destinations[k]|length) %}
> if $fromhost-ip == {{ client_groups[k] | to_json }} and {{
> client_destinations[k][i].var }} ==
> '{{ client_destinations[k][i].contains }}' then
> {{ client_destinations[k][i].destination }} {% endfor %}
> {% endif %}
> {% else %}
> if $fromhost-ip == {{ client_groups[k] | to_json }} then {{
> client_destinations[k].destination }}
> {% endfor %}
First: you should **really** indent stuff for better readability:
{% for k in client_groups %}
{% if client_destinations[k][0].var is defined %}
{% for i in range(0,client_destinations[k]|length) %}
if $fromhost-ip == {{ client_groups[k] | to_json }} and {{
client_destinations[k][i].var }} == '{{ client_destinations[k][i].contains
}}' then {{ client_destinations[k][i].destination }}
{% endfor %}
{% endif %}
{% else %}
if $fromhost-ip == {{ client_groups[k] | to_json }} then {{
client_destinations[k].destination }}
{% endfor %}
> The desired output is:
> [...]
>
> This means, the statement after {% else %} - if $fromhost-ip == {{
> client_groups[k] | to_json }} then
> {{ client_destinations[k].destination }}
> - is getting included in the `for` loop ({% for i in
> range(0,client_destinations[k]|length) %}); {% else %} is not being
> honored.
It is being honored. Please check the documentation:
http://jinja.pocoo.org/docs/2.10/templates/#for
The else branch of a for loop is executed only if the loop is over zero
elements. Since your loop is over a non-zero amount of elements, the
else branch of the for loop is ignored.
You probably don't want an else branch for the for loop, but for the if
construct. You can see from the indented version that you don't have
that right now.
Cheers,
Felix
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/20190211082026.5bb867c7%40rovaniemi.
For more options, visit https://groups.google.com/d/optout.