[BUG][Jinja2][value-assign] Can't assign value in a loop?

7 views
Skip to first unread message

Simon Jones

unread,
Jan 3, 2019, 3:30:34 AM1/3/19
to pocoo-libs
Hi all,

I'm using Jinja2 template to generate config files, but I found a BUG, which is I could not assign value in a loop, like this:

1) use this template, could generate 'uuusevrf'

{% block vrf_mgmt_interface %}
{% set usevrf = 'no' %}
{% set usevrf = 'nono' %}
{% if MGMT_INTERFACE %}
{% set uuusevrf = 'yes' %}
{%   for (name, prefix) in MGMT_INTERFACE %}
{%     if name == 'eth0' %}
{%       if MGMT_INTERFACE[(name, prefix)].has_key('vrfname') %}
{%         if prefix | ipv4 %}
USE_VRF
=2
IP_VERSION
=4
ETH_NAME
=eth0
ETH_IP
={{ prefix | ip }}
ETH_SUB_NET
={{ prefix | network }}/{{ prefix | prefixlen }}
ETH_LOOKUP_IP
={{ prefix | ip }}
VRFNAME
={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }}
GWADDR
={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }}
{%         else %}
USE_VRF
=2
IP_VERSION
=6
ETH_NAME
=eth0
ETH_IP
={{ prefix | ip }}
ETH_SUB_NET
={{ prefix | network }}/{{ prefix | prefixlen }}
ETH_LOOKUP_IP
={{ prefix | ip }}
VRFNAME
={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }}
GWADDR
={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }}
{%         endif %}
{%       else %}
USE_VRF
=1
{%       endif %}
{%     endif %}
{%   endfor %}
{% endif %}
{{ usevrf }}
{{ uuusevrf }}
{% endblock vrf_mgmt_interface %}


2) could generate 'uuusevrf', this is result:

rfaces.j211:~# sonic-cfggen -d -t /usr/share/sonic/templates/vrf_management_inter
USE_VRF
=2
IP_VERSION
=4
ETH_NAME
=eth0
ETH_IP
=172.18.8.211
ETH_SUB_NET
=172.18.8.0/24
ETH_LOOKUP_IP
=172.18.8.211
VRFNAME
=mgmtvrf
GWADDR
=172.18.8.1
nono
yes


2) use this template, could NOT generate 'uuusevrf'

{% block vrf_mgmt_interface %}
{% set usevrf = 'no' %}
{% set usevrf = 'nono' %}
{% if MGMT_INTERFACE %}
{%   for (name, prefix) in MGMT_INTERFACE %}
{% set uuusevrf = 'yes' %}
{%     if name == 'eth0' %}
{%       if MGMT_INTERFACE[(name, prefix)].has_key('vrfname') %}
{%         if prefix | ipv4 %}
USE_VRF
=2
IP_VERSION
=4
ETH_NAME
=eth0
ETH_IP
={{ prefix | ip }}
ETH_SUB_NET
={{ prefix | network }}/{{ prefix | prefixlen }}
ETH_LOOKUP_IP
={{ prefix | ip }}
VRFNAME
={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }}
GWADDR
={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }}
{%         else %}
USE_VRF
=2
IP_VERSION
=6
ETH_NAME
=eth0
ETH_IP
={{ prefix | ip }}
ETH_SUB_NET
={{ prefix | network }}/{{ prefix | prefixlen }}
ETH_LOOKUP_IP
={{ prefix | ip }}
VRFNAME
={{ MGMT_INTERFACE[(name, prefix)]['vrfname'] }}
GWADDR
={{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }}
{%         endif %}
{%       else %}
USE_VRF
=1
{%       endif %}
{%     endif %}
{%   endfor %}
{% endif %}
{{ usevrf }}
{{ uuusevrf }}
{% endblock vrf_mgmt_interface %}

4) could NOT generate, this is result:

rfaces.j211:~# sonic-cfggen -d -t /usr/share/sonic/templates/vrf_management_inter
USE_VRF
=2
IP_VERSION
=4
ETH_NAME
=eth0
ETH_IP
=172.18.8.211
ETH_SUB_NET
=172.18.8.0/24
ETH_LOOKUP_IP
=172.18.8.211
VRFNAME
=mgmtvrf
GWADDR
=172.18.8.1
nono

So, why ???

You could see that, same data, just the position of this line ({% set uuusevrf = 'yes' %}) is different. And the result show it definitely go into loop, but why this line NOT work ??? 

BTW, It's hard to debug !!!!!!

Simon Jones

unread,
Jan 3, 2019, 3:38:15 AM1/3/19
to pocoo-libs
And why do not support 'break' ......

在 2019年1月3日星期四 UTC+8下午4:30:34,Simon Jones写道:

Juan Pablo Scaletti

unread,
Jan 3, 2019, 10:12:45 AM1/3/19
to pocoo...@googlegroups.com
As you can read on the documentation, no, you can't assign a value in a
loop and use it outside the loop, unless you use a namespace:

http://jinja.pocoo.org/docs/2.10/templates/#assignments (see: Scoping
Behavior)


--
You received this message because you are subscribed to the Google Groups "pocoo-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pocoo-libs+...@googlegroups.com.
To post to this group, send email to pocoo...@googlegroups.com.
Visit this group at https://groups.google.com/group/pocoo-libs.
For more options, visit https://groups.google.com/d/optout.


--

Juan Pablo Scaletti

Simon Jones

unread,
Jan 7, 2019, 4:02:43 AM1/7/19
to pocoo-libs
Cool, but if this is a bug?

在 2019年1月3日星期四 UTC+8下午11:12:45,Juan Pablo Scaletti写道:
Reply all
Reply to author
Forward
0 new messages