以下代码有效,但输出是随机的。我想知道如何让python脚本按顺序(自上而下)读取并输出。
template.j2
{% for iface in ifaces %}
bridge-domain NAME-{{ ifaces[iface]['bviif'] }}
mac
aging
time 30000
!
limit
maximum 128000
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether501.{{ ifaces[iface][bviif] }}
dhcp ipv4 snoop profile UpLink
static-mac-address 0001
static-mac-address 0002
!
interface {{ iface }}
dhcp ipv4 snoop profile UN
split-horizon group
!
routed interface {{ ifaces[iface]['bviif'] }}
{% endfor %}
datavars.yml
ifaces:
GigabitEthernet100/0/0/10.232:
bviif: 232
GigabitEthernet100/0/0/10.233:
bviif: 233
GigabitEthernet100/0/0/10.234:
bviif: 234
GigabitEthernet100/0/0/10.253:
bviif: 253
GigabitEthernet100/0/0/10.254:
bviif: 254
GigabitEthernet100/0/0/10.255:
bviif: 255
GigabitEthernet100/0/0/10.256:
bviif: 256
GigabitEthernet100/0/0/10.257:
bviif: 257
[...] until 100/0/0/14.xxx
prerender.py
from jinja2 import Template
import yaml
import sys
from glob import glob
datavars_fname = sys.argv[1:1] or glob('*.yml')[0]
template_fname = sys.argv[2:2] or glob('*.j2')[0]
datavars = yaml.load(open(datavars_fname).read())
template = Template(open(template_fname).read())
print template.render(datavars)
使用该代码,我得到以下结果:
bridge-domain NAME-255
mac
aging
time 30000
!
limit
maximum 128000
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether501.255
dhcp ipv4 snoop profile UpLink
static-mac-address 0001
static-mac-address 0002
!
interface GigabitEthernet100/0/0/10.255
dhcp ipv4 snoop profile UN
split-horizon group
!
routed interface BVI255
bridge-domain Name-254
mac
aging
time 30000
!
limit
maximum 128000
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether701.254
dhcp ipv4 snoop profile UpLink
static-mac-address 0001
static-mac-address 0002
!
interface GigabitEthernet100/0/0/10.254
dhcp ipv4 snoop profile UN
split-horizon group
!
routed interface BVI254
[...] 如您所见,它从.255随机开始,即使在datavars.yml中,它也从.232开始。
预期结果:
bridge-domain NAME-232
mac
aging
time 30000
!
limit
maximum 128000
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether501.232
dhcp ipv4 snoop profile UpLink
static-mac-address 0001
static-mac-address 0002
!
interface GigabitEthernet100/0/0/10.232
dhcp ipv4 snoop profile UN
split-horizon group
!
routed interface BVI232
bridge-domain Name-233
mac
aging
time 30000
!
limit
maximum 128000
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether701.233
dhcp ipv4 snoop profile UpLink
static-mac-address 0001
static-mac-address 0002
!
interface GigabitEthernet100/0/0/10.233
dhcp ipv4 snoop profile UN
split-horizon group
!
routed interface BVI233
答案 0 :(得分:0)
我解决了安装oyaml并将其导入的问题。
将oyaml导入为yaml
谢谢!