我有以下地图列表:
variable "virtual_machines" {
default = {
"master1" = {
name = "z-ca-bdc-master1"
worker_node = false
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
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
},
我想做的是创建一个我可以分配给变量的虚拟机名称列表,其中包含 z-ca-bdc-master1、z-ca-bdc-master2 等。 . .我怎样才能做到这一点?
我的最终目标是能够将其与 template_file 一起使用:
data "template_file" "k8s" {
template = file("./templates/kubespray_inventory.tpl")
vars = {
k8s_master_host = values(var.virtual_machines)[*].name
k8s_node_host = values(var.virtual_machines)[*].name
}
}
答案 0 :(得分:0)
要从您的 var.virtual_machines
中获取“虚拟机名称列表”,您可以执行以下操作:
locals {
vm_names = values(var.virtual_machines)[*].name
}
然后您可以在需要时将其用作local.vm_names
。
更新:
locals {
vm_names = [for k,v in var.virtual_machines: v.name if v.worker_node]
}