我有以下复杂的字典(这只是一个示例)。我正在尝试获取属于 server1 的所有 id 的列表。server1 的名称大小写不一。
我尝试了类似match
,的 jinja2 过滤器search
,equalto
但它们都没有返回预期结果。还尝试了 JSON 查询,但仍然缺少如何将全部小写或大写进行比较。
---
- name: TEST
hosts: localhost
gather_facts: no
vars:
datacenters: {
cabinets: {
servers: [
{
name: Server1,
id: 1
},
{
name: SERVER1,
id: 2
},
{
name: Server2,
id: 3
},
{
name: server1,
id: 4
},
]
}
}
tasks:
- name: get ids for Server 1
set_fact:
ids: "{{ datacenters.cabinets.servers
| selectattr('name','match','Server1')
| map(attribute='id')
| list }}"
- debug:
var: ids
- debug: msg="{{ datacenters | json_query(\"cabinets.servers[?name == 'Server1'].id\") }}"