Hi Everyone,
I have content from an csv file which gets imported in data & local. The imported data is a map which contains a tuple (because of group mode …) which contains an object with various values.
I use this content to create resources for Cisco ACI which works as expected.
But there is one thing which currently grills my brain.
I need to do a cisco epg (End Point Group) mapping where I have to map a dynamic amount of epgs to one server. The epgs a specified in the inner object as follows: "start:stop / start:stop / and so on.
Example: “1:3/40:40/50:51” meaning all eps starting from epg 1 to epg 3, epg 40 and all epgs starting from epg 50 to epg 51 → 1,2,3,40,50,51 should be mapped to one server which is specified by the key of the map (v.host_name)
data "local_file" "hosts-csv" {
filename = "${path.module}/hosts.csv"
}
locals {
# read the data source and create array with host_name as key
hosts = csvdecode(data.local_file.hosts-csv.content)
hosts_profiles = { for k, v in local.hosts : v.host_name => v... }
}
> type(local.hosts_profiles)
object({
server1: tuple([
object({
acc_grp_policy: string,
// further output omitted
}),
]),
server2: tuple([
object({
acc_grp_policy: string,
// further output omitted
}),
]),
})
output "variable" {
value=local.hosts_profiles
}
+ variable = {
+ server1 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "1:3/40:40/50:51"
},
]
+ server2 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test02 1-15"
+ cdp = "cdp-enabled"
+ host_name = "test02"
+ epg = "30:30
},
]
}
The epg value for server1 is reading like this: All eps starting from epg 1 to epg 3, epg 40 and all epgs starting from epg 50 to epg 51 → 1,2,3,40,50,51
The epg value for server2 is reading like this: All eps starting from epg 30 to epg 30 → 30
The desired result should be a map which I can use to iterate with for_each looking like this:
+ mapToIterate = {
+ server1:1 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "1"
},
]
+ server1:2 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "2"
},
]
+ server1:3 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "3"
},
]
+ server1:30 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "30"
},
]
+ server1:50 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "50"
},
]
+ server1:51 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test01 1-10"
+ cdp = ""
+ host_name = "test01"
+ epg = "51"
},
]
+ server2:30 = [
+ {
+ acc_grp_policy = "auto"
+ accprtsel_name = "test02 1-15"
+ cdp = "cdp-enabled"
+ host_name = "test02"
+ epg = "30:30
},
]
}
I’ve managed to build a flattend list with a test value but unfortunately I’m not able to build a correct map with this syntax. In python I would pass the result from the first split as input to the next for loop but I’m grilling my brain how to do this in terraform.
My flattend test list looks like this:
epglist = distinct(flatten([
for entry in split("/", "1:3/40:40/50:51") : range(split(":", entry)[0],split(":", entry)[1] + 1)
]))
+ epglist_final = [
+ 1,
+ 2,
+ 3,
+ 40,
+ 50,
+ 51
]
Any help is highly appreciated.
Kind regards
Oliver