Python Forum

Full Version: Trying to use Jinja2 for loop with Python 3.6
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.