Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

terraform - iterating through list of maps with one loop and multiple string creation statements

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...