Python Forum
Trying to use Jinja2 for loop with Python 3.6
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to use Jinja2 for loop with Python 3.6
#1
Hello, I am getting errors with a simple for loop in Jinja which I think should work. My code:
{# Server Interface Template #}
{% for n in range(240) %}
  {% if n+1 < 16 %}
  interface GigabitEthernet2/0/{{ n+1 }}
    description {{ yml_vars.SERVER['description'] }}
    switchport mode {{ yml_vars.SERVER['mode'] }}
    switchport access vlan {{ yml_vars.SERVER['vlan_id'] }}
    spanning-tree portfast{{ yml_vars.SERVER['portfast'] }}
  {% elif n+1 > 16 and n < 19 %}
  interface GigabitEthernet2/0/{{ n+1 }}
    description {{ yml_vars.WIFI_PORT['description'] }}
    switchport mode {{ yml_vars.WIFI_PORT['mode'] }}
    switchport access vlan {{ yml_vars.WIFI_PORT['native'] }}
    spanning-tree portfast{{ yml_vars.WIFI_PORT['stp_speed'] }}
  {% endif %}
{% endfor %}
This gives the error:
File "/home/blamb/Python_Projects/3850_Replacement_Netmiko/template.j2", line 2, in ?
{% for n in range(240) %}

Syntax Error: invalid syntax: /home/blamb/Python_Projects/3850_Replacement_Netmiko/template.j2, line 2, pos 2
{% for n in range(240) %}

It fails before it gets to read the yml part: 'yml_vars'. This is to program a switch stack with 5 48 port switches equaling 240 ports. IF someone has a better way, PLEASE let me know. I am still VERY new to python as it is my first language ever for DevNet.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can set different Jinja2 version for different app? luofeiyu 1 992 Nov-27-2022, 11:58 AM
Last Post: snippsat
  Jinja2 and Excel zinho 1 4,478 Mar-26-2021, 12:27 AM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020