我正在尝试使用 jmesquery 在 Ansible 中的给定 JSON 中获取满足特定条件的接口名称。
问题是列表中的项目已命名,它不是包含其中每个项目的所有值的列表,并且无法弄清楚如何过滤它。
在 vlan == "1" 的条件下,此 JSON 应提供类似['GigabitEthernet0/0', 'GigabitEthernet0/1'] 的输出
使用 dict2items (如@mdaniel 建议的那样)后,我设法使用条件应用过滤器,但我正在努力检索现在在key中的接口名称:
- name: PRINT
debug:
msg: "{{ jsondata.interfaces | dict2items | json_query( query ) }}"
vars:
query: >-
[][value][?vlan=='1']
JSON
interfaces: {
"GigabitEthernet0/0": {
"duplex_code": "auto",
"port_speed": "a-100",
"status": "connected",
"type": "10/100/1000BaseTX",
"vlan": "1"
},
"GigabitEthernet0/1": {
"duplex_code": "auto",
"port_speed": "a-100",
"status": "connected",
"type": "10/100/1000BaseTX",
"vlan": "1"
}
}
应用 jsondata.interfaces 后的 JSON | dict2items
[
{
"key": GigabitEthernet0/0",
"value": {
"duplex_code": "auto",
"port_speed": "a-100",
"status": "connected",
"type": "10/100/1000BaseTX",
"vlan": "1"
}
},
{
"key": GigabitEthernet0/1",
"value": {
"duplex_code": "auto",
"port_speed": "a-100",
"status": "connected",
"type": "10/100/1000BaseTX",
"vlan": "1"
}
}
]
先感谢您!