Trying to use Jinja2 for loop with Python 3.6 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Trying to use Jinja2 for loop with Python 3.6 (/thread-24402.html) |
Trying to use Jinja2 for loop with Python 3.6 - benniehanas - Feb-12-2020 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. |