Hello
I want to for_each over a map, create a data resource for each item in the map
then join the data resources into a list so I can write it to a local file resource.
Lets keep it simple for this example. The join below works if I use count but I need
to use for_each for other reasons.
hosts.tpl
${hostname}
My data block where var.systems is keyed on hostname.
data "template_file" "hosts" {
for_each = {
for k, v in var.systems : k => v
}
template = file("${path.module}/hosts.tpl")
vars = {
hostname = each.key
}
}
I create all the data resources for all my hosts then I want to create a list of them and I will use another template file and create a local file resource with the list of hosts.
host_list.tpl
${hosts}
My other data block
data "template_file" "host_list" {
for_each = {
for k, v in var.systems : k => v
}
template = file("${path.module}/host_list.tpl")
vars = {
hosts = join("", data.template_file.hosts.*.rendered) # <<< how do I join all my data values into a list?
}
}
I get the error
Error: Unsupported attribute
│
│ on hosts/main.tf line 136, in data "template_file" "host_list":
│ 136: hosts = join("", data.template_file.hosts.*.rendered)
│
│ This object does not have an attribute named "rendered".
–
Thank you