Reputation: 23
I am trying to compose a list from a list of dicts of lists but am failing. Trying to get a list of all peers to r1
Example data yaml:
---
conn:
- id: asd
peers:
- name: r1
- name: r2
- id: dsa
peers:
- name: r1
- name: r3
- id: sad
peers:
- name: r2
- name: r4
The output should be [r2,r3]
as r1 only has r2 and r3 as peers. How can I get this list from an ansible task?
Upvotes: 0
Views: 826
Reputation: 68269
You may want to explore JMESPath:
---
- hosts: localhost
gather_facts: no
vars:
conn:
- id: asd
peers:
- name: r1
- name: r2
- id: dsa
peers:
- name: r1
- name: r3
- id: sad
peers:
- name: r2
- name: r4
tasks:
- debug:
msg: "{{ conn | json_query(qry) }}"
vars:
qry: '[*].{p:peers[].name} | [?contains(p,`r1`)].p[] | [?@!=`r1`]'
Select peers[].name
as p
, select only items which have r1
as element of p
, then flatten list and drop any r1
items effectively leaving only r1 peers as the result.
Upvotes: 1