I have a list of defined objects in a YAML file. Some of them have themselves lists of objects. I cannot find a way to iterate on those. The YAML input file looks like this:
vpns:
- name: Test
template: template_name
ip_type: ip
ip_remote: 1.1.1.1
firewall_interface: vlan.xxxx
local_ip: 2.2.2.2
ikev2_profile: high_secure
ipsec_profile: secure-Test
tunnel_interface: tunnel.1
pre_shared_key: Redacted
- name: other_test
template: template_name
ip_type: ip
ip_remote: 3.3.3.3
firewall_interface: vlan.xxxx
local_ip: 2.2.2.2
ikev2_profile: high_secure
ipsec_profile: secure-Test
tunnel_interface: tunnel.2
pre_shared_key: Redacted
proxy_ids:
- name: smth
local: 10.111.0.0/24
remote: 10.0.128.0/24
protocol_any: true
- name: smth_else
local: 10.123.1.0/24
rempote: 10.0.213.0/24
protcol_any: true
Now importing the YAML file with this (the pre_shared keys are encrypted with SOPS) work well:
data "sops_file" "vpns" {
source_file = var.vpn_file
}
locals {
tunnels = yamldecode(data.sops_file.vpns.raw).vpns
}
But I cannot find a way to iterate on the proxy_ids. Plus, when configuring the proxy_ids, I need to access the template and the name
resource "panos_panorama_ipsec_tunnel_proxy_id_ipv4" "proxy_id" {
for_each = {for e in local.tunnels: [for proxy_id in local.tunnels[e]: proxy_id.name => proxy_id] => e}
template = each.tunnel.template.name
name = each.proxy_id.name
.......
}
Along with several other ways to flatten the data, but obviously without any success.
Not really sure what is your iteration goal, as your example is incomplete. But if you want to iterate over only those elements which have proxy_ids, then you can filter out them as follows into new local variable called tunnels_with_proxy:
locals {
tunnels = yamldecode(file("vpn.yaml"))["vpns"]
tunnels_with_proxy = {for idx,tunnel in local.tunnels:
idx=>tunnel if contains(keys(tunnel), "proxy_ids")}
}
Then, in your resources, you can use for_each only for tunnels with proxy_ids. Similarly, you can do local variable without proxy_ids. The separation of the original array into two arrays, one with and second without proxy_ids, will probably simply further operations on them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With