简体   繁体   English

在Python中借用Jinja2模板时如何遍历YAML列表/字典项

[英]How to Iterate through YAML list/dictionary items while rending Jinja2 template in Python

I seem to get stuck either with code or concept and need some help in iterating through YAML based dict/list while rendering Jinja2 Template in Python. 我似乎陷入了代码或概念的束缚,并且在用Python渲染Jinja2模板时需要一些帮助来遍历基于YAML的字典/列表。 I am a bit new in programming (3 weeks) so apologies in advance for any blunders. 我在编程方面有点新手(3周),因此对于任何错误,请提前道歉。

For instance I have the following dictionary defined in YAML. 例如,我在YAML中定义了以下字典。

---
- hostname: R1
  interfaces:
    - name: f0/0
      description: This is FastEth 0/0 connected to R2 FastEth 0/0
    - name: f0/1
      description: This is FastEth 0/1 connected to Local Host Loopback
- hostname: R2
  interfaces:
    - name: f0/0
      description: This is FastEth 0/0 connected to R2 FastEth 0/0
    - name: f0/1
      description: This is FastEth 0/1 connected to Local Host Loopback

and the following is the Jinja Template I am rendering: 以下是我正在渲染的Jinja模板:

{% for iface in config.interfaces %}
int {{ config.name }}
description {{ config.description }}
{% endfor %}

.... I have two routers R1 and R2 on which I want to send the rendered configs based on their dictionaries only. ....我有两个路由器R1和R2,我只想在它们的字典上发送渲染的配置。 I want to generate two config sets, one for each router. 我想生成两个配置集,每个路由器一个。 Hence I thought of doing this is Python but with no luck. 因此,我认为这样做是Python但没有运气。

env = Environment(loader=FileSystemLoader('./templates'),trim_blocks=True)
with open('./YAML/configuration.yml') as _:
    config_commands_var = yaml.load(_)

for device in range(len(devices)):
    print "\nStart time: " + str(datetime.now())
    username = devices[device]['username']
    password = devices[device]['password']
    ip = devices[device]['ip']
    device_type = devices[device]['device_type']
    secret = devices[device]['secret']
    hostname = devices[device]['hostname']

    config_commands = template.render(config=config_commands_var)
    push_config_commands(username, password, ip, device_type, secret, config_commands)

Here "devices" is a list of dictionaries of devices. 这里的“设备”是设备字典的列表。 and "push_config_commands" function send list of commands at once to any device. 和“ push_config_commands”函数立即将命令列表发送到任何设备。

I hope to really get my problem solved here, so will appreciate any sort of help. 我希望在这里能真正解决我的问题,因此将不胜感激。 Thanks in advance. 提前致谢。

I think I got it working. 我想我可以使用了。 However any suggestions are still welcomed. 但是,仍然欢迎任何建议。

Changed the YAML file as: 将YAML文件更改为:

---
interfaces:
  R1:
    f0/0:
      description: This is FastEth 0/0 connected to R2 FastEth 0/0
    f0/1:
      description: This is FastEth 0/1 connected to Local Host Loopback
  R2:
    f0/0:
      description: This is FastEth 0/0 connected to R2 FastEth 0/0
    f0/1:
      description: This is FastEth 0/1 connected to Local Host Loopback

I changed the template as: 我将模板更改为:

{% for iface in config %}
interface {{ iface }}
description {{ config[iface]['description'] }}
{% endfor %}

and while rendering I passed keys to look at: 在渲染时,我传递了一些键来查看:

config_commands = template.render(config=config_commands_var['interfaces'][hostname])

Any improvements are more than welcomed. 任何改进都值得欢迎。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM