Jinja2/Ansible 中的嵌套变量
我正在努力理解如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下命令将循环其他所有 vlan,以等于当前
inventory_hostname
的 switch 键,过滤掉lags
列表中不包含1
的所有 vlan:这里有 2 个变体,因为我不知道你到底想在
lags
键中寻找什么。保留滞后 1 或 2 的 VLAN:
仅保留滞后 1 和 2 的 VLAN:
The following will loop other all vlans for the switch key equal to current
inventory_hostname
filtering out all vlans which do no contain1
in thelags
list: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:Only keep vlans which are lags 1 and 2: