Jinja2/Ansible 中的嵌套变量

发布于 2025-01-09 19:20:25 字数 1060 浏览 0 评论 0原文

我正在努力理解如何在 Jinja2/Ansible 中使用嵌套变量

我在 yaml 文件中有以下变量

dual_homed_workloads:
  switch1:
    - tag: 200
      description: DB-Servers
      ports:
      lags: [1,2,4]
    - tag: 201
      description: Storage
      ports:
      lags: [1,2,4]
    - tag: 202
      description: iLo
      ports:
      lags: [1,3,4]
  switch2:
    - tag: 200
      description: DB-Servers
      ports:
      lags: [3,4]
    - tag: 211
      description: voice
      ports:
      lags: [1,]
    - tag: 2000
      description: egree
      ports:
      lags: [2,3]

,我想搜索与 inventory_hostname 匹配的交换机名称,然后获取 VLAN 的标签

{% for vlan, switch in dual_homed_workloads %}
{% if switch == inventory_hostname %}
VLAN {{ vlan.tag }}
  name {{ vlan.descrption }}
{% endif %}
{% endfor %}

如果我要为交换机 1 运行此命令,我需要如下输出

vlan 200
 name DB-Servers
vlan 201
 name Storage
vlan 202
 name iLo

另外,LAG 是一个列表,是否有一种方法可以在该列表中搜索值,例如 "1"

谢谢

I am struggling to understand how to use nested variables in Jinja2/Ansible

I have the following Variables in a yaml file

dual_homed_workloads:
  switch1:
    - tag: 200
      description: DB-Servers
      ports:
      lags: [1,2,4]
    - tag: 201
      description: Storage
      ports:
      lags: [1,2,4]
    - tag: 202
      description: iLo
      ports:
      lags: [1,3,4]
  switch2:
    - tag: 200
      description: DB-Servers
      ports:
      lags: [3,4]
    - tag: 211
      description: voice
      ports:
      lags: [1,]
    - tag: 2000
      description: egree
      ports:
      lags: [2,3]

and I want to search for the switch name matching the inventory_hostname, then get the tag of the VLAN

{% for vlan, switch in dual_homed_workloads %}
{% if switch == inventory_hostname %}
VLAN {{ vlan.tag }}
  name {{ vlan.descrption }}
{% endif %}
{% endfor %}

If I was to run this for switch 1 I'd want an output as follows

vlan 200
 name DB-Servers
vlan 201
 name Storage
vlan 202
 name iLo

Also the LAGs is a list, is there a way of searching that list for a value e.g. "1"

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2025-01-16 19:20:25

以下命令将循环其他所有 vlan,以等于当前 inventory_hostname 的 switch 键,过滤掉 lags 列表中不包含 1 的所有 vlan:

{% for vlan in dual_homed_workloads[inventory_hostname] if 1 in vlan.lags %}
VLAN {{ vlan.tag }}
  name {{ vlan.description }}
{% endfor %}

这里有 2 个变体,因为我不知道你到底想在 lags 键中寻找什么。

保留滞后 1 或 2 的 VLAN:

{% for vlan in dual_homed_workloads[inventory_hostname] if vlan.lags | intersect([1,2]) | length > 0 %}
VLAN {{ vlan.tag }}
  name {{ vlan.description }}
{% endfor %}

仅保留滞后 1 和 2 的 VLAN:

{% set keep_lags = [1,2] %}
{% for vlan in dual_homed_workloads[inventory_hostname] if vlan.lags | intersect(keep_lags) | length == keep_lags | length %}
VLAN {{ vlan.tag }}
  name {{ vlan.description }}
{% endfor %}

The following will loop other all vlans for the switch key equal to current inventory_hostname filtering out all vlans which do no contain 1 in the lags list:

{% for vlan in dual_homed_workloads[inventory_hostname] if 1 in vlan.lags %}
VLAN {{ vlan.tag }}
  name {{ vlan.description }}
{% endfor %}

Here are 2 variations since I don't know exactly what you intend to look for inside the lags key.

Keep vlans which are in either lags 1 or 2:

{% for vlan in dual_homed_workloads[inventory_hostname] if vlan.lags | intersect([1,2]) | length > 0 %}
VLAN {{ vlan.tag }}
  name {{ vlan.description }}
{% endfor %}

Only keep vlans which are lags 1 and 2:

{% set keep_lags = [1,2] %}
{% for vlan in dual_homed_workloads[inventory_hostname] if vlan.lags | intersect(keep_lags) | length == keep_lags | length %}
VLAN {{ vlan.tag }}
  name {{ vlan.description }}
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文