0

I have a loop where I have to set two consecutive numbers. The first number is the loop counter and the next one is the loop counter + 1. In the next loop iteration, I would like for it to continue from the last number used. It keeps picking up the next loop counter not the + 1. And not sure why.

I have tried to set the variable on the top of the loop and at the end and still getting the same results.

Example:
precedence = 3
att_ip = ["1.1.1.1", "2.2.2.2"]
expected results:
 precedence is 3
 precedence plus index 3
 precedence plus index + 1 is  4
 
 precedence is 5
 precedence plus index 5
 precedence plus index + 1 is  6
 
Actual results:
 precedence is 3
 precedence plus index 3
 precedence plus index + 1 is  4

 precedence is 3
 precedence plus index 4
 precedence plus index + 1 is  5
{% for att_ip in att_ips %}
{% set precedence = precedence + 1 %}

precedence is {{ precedence }}
precedence plus index {{ precedence + loop.index0 }}
precedence plus index +1 is  {{ precedence + loop.index }}


{% endfor %}

1 Answers1

0

You can't set a variable in a loop like that; see the Jinja documentation on scoping behavior in the assignments section:

Please keep in mind that it is not possible to set variables inside a block and have them show up outside of it. This also applies to loops. The only exception to that rule are if statements which do not introduce a scope. As a result the following template is not going to do what you might expect:

{% set iterated = false %}
{% for item in seq %}
    {{ item }}
    {% set iterated = true %}
{% endfor %}
{% if not iterated %} did not iterate {% endif %}

It is not possible with Jinja syntax to do this

There are some ways to work around this, but isn't {{ precedence + loop.index }} always going to give you the value you want?


You could do this:

{% set initial_precedence = precedence %}
{% for att_ip in att_ips %}
{% set precedence = initial_precedence + loop.index %}

precedence is {{ precedence }}
precedence plus index {{ precedence + loop.index0 }}
precedence plus index +1 is  {{ precedence + loop.index }}
{% endfor %}

This will produce as output:


precedence is 4
precedence plus index 4
precedence plus index +1 is  5

precedence is 5
precedence plus index 6
precedence plus index +1 is  7
larsks
  • 277,717
  • 41
  • 399
  • 399
  • loop.index is set correctly, my issue is that I need to figure out how to set the precedence variable and not sure how do this. – devops-guy021 Dec 13 '22 at 18:52
  • Can't you just `{% set localprecedence = precedence + loop.index %}`? – larsks Dec 13 '22 at 18:57
  • This still give me the same result nothing changed – devops-guy021 Dec 13 '22 at 19:44
  • I guess I'm not clear on what you want. It seems as if the value of `{{ precedence + loop.index }}` is *exactly* the value you're looking for, and it's not clear from your question (or your comments) why you can't simply use the expression. – larsks Dec 13 '22 at 19:45
  • I want the next iteration of precedence. `{{ precedence + loop.index }}` is working but this is not what I want. On every iteration precedence does not change. How can I set precedence inside the loop? – devops-guy021 Dec 13 '22 at 19:59
  • I've updated this answer with one option for setting `precedence` inside the loop. – larsks Dec 13 '22 at 20:08