次の複雑な辞書があります(これは単なるサンプルです)。そして、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\") }}"