在YAML中存储多个网络设备并在jinja2中引用它

时间:2018-06-30 16:02:22

标签: yaml jinja2

如何构造yaml文件来容纳多个网络设备并从jinja2文件中引用特定的供应商/类型?

例如:

—
ip: 10.10.10.10
hostname: core-fw-domain-location
username: admin
password: password
vendor: cisco
type: firewall
ip: 20.20.20.20
hostname: core-rt-domain-location
username: admin
password: password
vendor: cisco
type: router

以上内容显然不起作用-如何构造它,使其具有层次结构?

在我的jinja2文件中,如何引用它?

例如:

{% if node.hostname == 'core-fw-domain-location' %}
enable secret {{ node.secret }}
username admin privilege password {{ node.secret }}
{% endif %}

1 个答案:

答案 0 :(得分:1)

将YAML构造为地图列表:

- ip: 10.10.10.10
  hostname: core-fw-domain-location
  username: admin
  password: password
  vendor: cisco
  type: firewall
- ip: 20.20.20.20
  hostname: core-rt-domain-location
  username: admin
  password: password
  vendor: cisco
  type: router

然后在渲染模板时,将python node更改为nodes,因为现在配置了多个节点:

config = baseline.render(nodes = node_object)

然后在您的jinja2文件中,依次遍历nodes

{% for node in nodes -%}
{% if node.hostname == 'core-fw-domain-location' %}
enable secret {{ node.password }}
username admin privilege password {{ node.password }}
{% endif %}
{%- endfor %}