I have following complex dictionary (this is just a sample). And I'm trying to grab a list of all ids which belong to server1. Name of the server1 is in mixed cases.
I tried jinja2 filters like match
, search
, equalto
but none of them returns expected result. Also tried JSON query but still missing how to lower or upper case all for compare to work.
---
- 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\") }}"