我试图通过创建基于预定义变量的开关的接口配置来循环,我知道我可以在Jinja中这样做,但我不确定如何开始实现它。
我的模板如下。通过python脚本,我将传递2个值,开关的数量和端口的数量。
因此,如果有三个开关--24个端口开关--我需要它为端口g1/0/1到g1/0/24、g2/0/1到g2/0/24和g3/0/1至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 %}发布于 2016-04-20 15:33:34
我已经成功地实现了我的结果,我动态地创建了一个数组,它将列出所有的端口,然后在这个数组上迭代,它完美地工作。不确定是否有更好的方法,但这暂时是好的
谢谢
创建数组
#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 %}https://stackoverflow.com/questions/36746221
复制相似问题