I have the following code:
locals {
all_nodes_verbose_etcd = [for k, v in var.virtual_machines:
format("%s ip=%s etcd_instance=%s", v.name, v.ipv4_address, v.etcd_instance)
if length(v.etcd_instance) > 0]
all_nodes_verbose = [for k, v in var.virtual_machines:
format("%s ip=%s", v.name, v.ipv4_address)
if length(v.etcd_instance) == 0]
master_nodes = [for k, v in var.virtual_machines:
v.name
if v.worker_node != true]
etcd_nodes = [for k, v in var.virtual_machines:
v.name
if length(v.etcd_instance) > 0]
}
Is there anyway this can be changed such that I only need one for loop ?
My virtual_machines list of maps looks like this (not the whole thing - but enough to provide an idea of what it looks like):
variable "virtual_machines" {
default = {
"master1" = {
name = "z-ca-bdc-master1"
worker_node = false
etcd_instance = "etcd1"
ipv4_address = "192.168.113.79"
ipv4_netmask = "22"
ipv4_gateway = "192.168.112.1"
dns_server = "192.168.112.2"
ram = 8192
logical_cpu = 4
disk0_size = 40
disk1_size = 0
},
"master2" = {
name = "z-ca-bdc-master2"
worker_node = false
etcd_instance = "etcd2"
ipv4_address = "192.168.113.80"
ipv4_netmask = "22"
ipv4_gateway = "192.168.112.1"
dns_server = "192.168.112.2"
ram = 8192
logical_cpu = 4
disk0_size = 40
disk1_size = 0
},
This is psuedo code for what I would like:
locals {
for k, v in var.virtual_machines:
all_nodes_verbose_etcd = [format("%s ip=%s etcd_instance=%s", v.name, v.ipv4_address, v.etcd_instance)
if length(v.etcd_instance) > 0]
all_nodes_verbose = [format("%s ip=%s", v.name, v.ipv4_address)
if length(v.etcd_instance) == 0]
master_nodes = [v.name
if v.worker_node != true]
etcd_nodes = [v.name
if length(v.etcd_instance) > 0]
}
question from:
https://stackoverflow.com/questions/65952133/iterating-through-list-of-maps-with-one-loop-and-multiple-string-creation-statem 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…