I would like to to get the array name (like DNS_One, NTP, etc.), if a particular key value (here cidr) from an input file matches.
I have tried with the below playbook, but that is causing errors.
Input.yaml:
[
{
"id": "111177789966",
"cidr": "10.10.10.10/24"
},
{
"id": "13215464897",
"cidr": "10.100.100.0/24"
}
]
Input_var.yaml:
---
Stores:
- name: "Test1"
DNS_One:
- "10.10.10.10/24"
- "192.168.1.1/24"
DNS_One-HO:
- "10.20.25.100"
- "10.100.100.0/24"
- name: "Test2"
DNS_One:
- "10.20.10.10/24"
Expected output:
[
{
"id": "111177789966",
"cidr": "10.10.10.10/24",
"net_name": "DNS_One"
},
{
"id": "13215464897",
"cidr": "10.100.100.0/24",
"net_name": "DNS_One-HO"
}
]
Playbook:
- name: Search
vars:
test-merge: >-
{{
input_var
| selectattr('name', 'eq', Test1 | string )
| map(attribute= item)
| first
| default([])
}}
set_fact:
po_g: >-
{{
po_g | default([]) +
[{
'id': item.id,
'cidr': item.cidr,
'subnetname': test-merge
}]
}}
loop: "{{ input }}"