Ansible 如何在jinja2模板中使用嵌套变量

Ansible 如何在jinja2模板中使用嵌套变量,ansible,jinja2,Ansible,Jinja2,如何在jinja模板中使用项目的嵌套变量?我正在运行ansible为网络交换机生成配置 试图使if语句基于bgp_peer_版本 我有以下变量文件 --- switches: - ansible_hostname: Core1 bgp_neighbors: - bgp_peer_version: both bgp_peer_ipv4: 10.1.1.1 bgp_peer_ipv6: 2001::1 bgp_v

如何在jinja模板中使用项目的嵌套变量?我正在运行ansible为网络交换机生成配置

试图使if语句基于bgp_peer_版本

我有以下变量文件

 ---
 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

问:“如何在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 %}
编辑问题、生成问题、添加预期结果、正确设置问题格式并删除此评论。