Python - Jinja2 - 循环计数
Python - Jinja2 - loop with counts
我正在尝试循环创建基于预定义变量的交换机接口配置,我知道我可以在 Jinja 中做到这一点,但我不确定如何开始实现它
我的模板如下。
通过我的 python 脚本,我将传递 2 个值,交换机数量和端口数量
所以如果有三个交换机 24 端口交换机,我将需要它来为端口 g1/0/1 到 g1/0/24、g2/0/1 到 g2/0/24 和 [= 创建一个配置26=] 到 24.
这有意义吗?我需要对这两个数字进行计数,直到它们满足设置的值
谢谢
亚历克斯
{% for %}
interface GigabitEthernet{{ SW }}/0/{{ Port }}
switchport access vlan {{ DATAVLAN }}
switchport mode access
switchport voice vlan {{ VOICEVLAN }}
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security aging time 1
switchport port-security aging type inactivity
switchport port-security
spanning-tree portfast
spanning-tree bpduguard enable
{% endfor %}
我已经设法实现了我的结果,我动态创建了一个数组来列出所有端口,然后遍历该数组,它工作得很好。不确定是否有更好的方法,但目前这个方法很好
谢谢
创建数组
#create interface list
intSwitchCount = int(row['SWITCHES'].strip())
intPortCount = int(row['PORTS'].strip())
intSW = 0
intPort = 0
lstPorts = []
while intSW != intSwitchCount:
intSW += 1
while intPort != (intPortCount) :
intPort += 1
strPort = '%s/0/%s' % (str(intSW),str(intPort))
lstPorts.extend([strPort])
intPort = 0
模板
{% for item in SWITCHPORTS %}
interface GigabitEthernet{{ item }}
switchport access vlan {{ DATAVLAN }}
switchport mode access
switchport voice vlan {{ VOICEVLAN }}
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security aging time 1
switchport port-security aging type inactivity
switchport port-security
spanning-tree portfast
spanning-tree bpduguard enable
{% endfor %}
我正在尝试循环创建基于预定义变量的交换机接口配置,我知道我可以在 Jinja 中做到这一点,但我不确定如何开始实现它
我的模板如下。 通过我的 python 脚本,我将传递 2 个值,交换机数量和端口数量
所以如果有三个交换机 24 端口交换机,我将需要它来为端口 g1/0/1 到 g1/0/24、g2/0/1 到 g2/0/24 和 [= 创建一个配置26=] 到 24.
这有意义吗?我需要对这两个数字进行计数,直到它们满足设置的值
谢谢 亚历克斯
{% for %}
interface GigabitEthernet{{ SW }}/0/{{ Port }}
switchport access vlan {{ DATAVLAN }}
switchport mode access
switchport voice vlan {{ VOICEVLAN }}
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security aging time 1
switchport port-security aging type inactivity
switchport port-security
spanning-tree portfast
spanning-tree bpduguard enable
{% endfor %}
我已经设法实现了我的结果,我动态创建了一个数组来列出所有端口,然后遍历该数组,它工作得很好。不确定是否有更好的方法,但目前这个方法很好
谢谢
创建数组
#create interface list
intSwitchCount = int(row['SWITCHES'].strip())
intPortCount = int(row['PORTS'].strip())
intSW = 0
intPort = 0
lstPorts = []
while intSW != intSwitchCount:
intSW += 1
while intPort != (intPortCount) :
intPort += 1
strPort = '%s/0/%s' % (str(intSW),str(intPort))
lstPorts.extend([strPort])
intPort = 0
模板
{% for item in SWITCHPORTS %}
interface GigabitEthernet{{ item }}
switchport access vlan {{ DATAVLAN }}
switchport mode access
switchport voice vlan {{ VOICEVLAN }}
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security aging time 1
switchport port-security aging type inactivity
switchport port-security
spanning-tree portfast
spanning-tree bpduguard enable
{% endfor %}