如何在jinja模板中使用项目的嵌套变量?我正在运行ansible来为网络交换机生成配置。
尝试使if语句基于bgp_peer_version,
我有以下变量文件
---
switches:
- ansible_hostname: Core1
bgp_neighbors:
- bgp_peer_version: both
bgp_peer_ipv4: 10.1.1.1
bgp_peer_ipv6: 2001::1
bgp_vrf:
- bgp_peer_version: v4
bgp_peer_ipv4: 10.1.1.2
bgp_peer_ipv6:
bgp_vrf:
- ansible_hostname: Core2
bgp_neighbors:
- bgp_peer_version: 'both'
bgp_peer_ipv4: 10.1.1.2
bgp_peer_ipv6: 2001::1
bgp_vrf:
- bgp_peer_version: 'v4'
bgp_peer_ipv4: 10.1.1.2
bgp_peer_ipv6:
bgp_vrf: 到目前为止,我有以下代码
{% if item[bgp_peer_version] == "v4" %}
BGP Peer
IPv4 address {{ bgp_peer_ipv4 }}
{% else %}
BGP Peer
IPv4 address {{ bgp_peer_ipv64 }}
IPv6 address {{ bgp_peer_ipv6 }}
{% endif %}我正在尝试构建一个模板,该模板将bgp邻居中的变量用于每个交换机。我正在寻找第一个模板的输出。模板1输出
BGP对等IPv4地址10.1.1.1 IPv6地址2001::1 BGP对等IPv4地址10.1.1.1
模板2输出
BGP对等IPv4地址10.1.1.2 IPv6地址2001::2 BGP对等IPv4地址10.1.1.1
发布于 2019-10-11 05:30:32
Q:“如何在jinja模板中使用(项目的)嵌套变量?”
这可能是你要找的东西吧?
{% for item1 in switches %}
{% for item in item1 %}
{% if item[bgp_peer_version] == "v4" %}
result is V4
{% else %}
result if both
{% endif %}
{% endfor %}
{% endfor %}https://stackoverflow.com/questions/58330777
复制相似问题