[英]how to generate config using jinja2 and a json file in python
i have a json file vlans.json
which contains the following我有一个 json 文件vlans.json
包含以下内容
{"1": {"description": "default", "name": "default"}, "2": {"description": "ilo", "name": "ILO"}}
and based on this info i'm trying to generate a config using some jinja2 template that should generate an output like并基于此信息,我正在尝试使用一些 jinja2 模板生成配置,该模板应生成 output 之类的
#
vlan 1
description default
name default
#
vlan 2
description ilo
name ilo
#
any idea how the code for this one should look like?知道这个代码应该是什么样子吗?
so far i have this code, but nothing works...到目前为止我有这个代码,但没有任何效果......
from jinja2 import Template
import json
vlans_file = "vlans.json"
vlan_template = '''
vlan {{ vlans.id }}
description {{ vlans.description }}
name {{ vlans.name }}
#
'''
with open(vlans_file) as json_file:
vlans = json.load(json_file)
for key in vlans:
vlan_config = vlan_template.render(vlans)
i made some progress我取得了一些进展
from jinja2 import Template
import json
vlans_file = "vlans.json"
with open(vlans_file) as json_file:
vlans = json.load(json_file)
vlan_template = Template('''
{% for vlan in vlans %}
#
vlan {{ vlan }}
description {{ value }}
#
{% endfor %}
''')
print(vlan_template.render(vlans = vlans))
and it prints它打印
#
vlan 1
description
#
#
vlan 2
description
#
unfortunately i dunno how to get to the output below不幸的是,我不知道如何到达下面的 output
#
vlan 1
description default
name default
#
vlan 2
description ilo
name ilo
#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.