jinja2 - читать элементы из yml по порядку - PullRequest
0 голосов
/ 03 июля 2019

Следующий код работает, но вывод рандомизирован.Я хотел бы знать, как позволить скрипту 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

1 Ответ

0 голосов
/ 03 июля 2019

Я решил это, установив oyaml и импортировав его.

импорт oyaml как yaml

Спасибо!

...