嵌套列表中的 Terraform 嵌套循环 [英] Terraform nested loop in nested list
问题描述
我正在尝试创建一种动态方法,以在将由最终用户配置的多个环境中创建虚拟机.尝试使用嵌套循环进行循环.扁平化函数、计数等,但还没有找到达到我目标的方法.我有以下结构的 terrafrom.tfvars:
I'm trying to create a dynamic method to create vms in multiple environments that will be configured by the end user. Tried for loops with nested loops. flatten function, count, etc but haven't found a way to reach my goal. I have terrafrom.tfvars with the follwing structure:
Bigip_devices = {
main_hub = {
region = "eastus"
azs = ["1"] #Azure availabilty zones
vnet_name = "vnet-main" # Vnet name to deploy to
bigip_instance_count = 2 # Number of instnaces to delpoy
cluster = "yes" # Deploy as a cluster or stand alone device
version = "" # Leave blank for default value
sku = "" # Leave blank for default value - f5-bigip-virtual-edition-25m-best-hourly
offer = "" # Leave blank for default value - f5-big-ip-best
instance_type = "" # Leave blank for default value - Standard_DS3_v2
disable_password_authentication = "" #Leave blank for default value
tags = ""
}
spoke = {
region = "eastus"
azs = ["1","2"] #Azure availabilty zones
vnet_name = "vnet-spoke" # Vnet name to deploy to
bigip_instance_count = 4 # Number of instnaces to delpoy
cluster = "yes" # Deploy as a cluster or stand alone device
version = "" # Leave blank for default value
sku = "" # Leave blank for default value - f5-bigip-virtual-edition-25m-best-hourly
offer = "" # Leave blank for default value - f5-big-ip-best
instance_type = "" # Leave blank for default value - Standard_DS3_v2
disable_password_authentication = "" #Leave blank for default value
tags = ""
}
}
迭代列表中的每个键(在示例中是 2 个键 - 主集线器和辐条)并创建与 bigip_instance_count 设置相对应的虚拟机数量的正确方法是什么.在上面的例子中,我想创建 2 个环境,一个有 2 个设备,第二个有 4 个设备.有没有办法实现它?
What is the correct method to iterate each key in the list( in the example the are 2 keys - main hub and spoke) and to create the amount of virtual machines corresponding to the bigip_instance_count setting. In the above example, I want to create 2 environments, one with 2 devices and the second with 4 devices. Is there a way to achieve it?
推荐答案
如果您将上述复杂的 JSON 转换为一个集合,其中每个要创建的资源都有一个元素,那将会非常方便.为此,您可以使用 flatten
函数.
It would be really convenient if you transform the above complex JSON into a collection that has one element per resource you want to create. To achieve this, you could use the flatten
function.
locals {
# A list of objects with one object per instance.
flattened_values = flatten([
for ip_key, ip in var.Bigip_devices : [
for index in range(ip.bigip_instance_count) : {
region = ip.region
azs = ip.azs
ip_index = index
ip_key = ip_key
cluster = ip.cluster
version = ip.version
sku = ip.sku
offer = ip.offer
instance_type = ip.instance_type
disable_password_authentication = ip.disable_password_authentication
tags = ip.tags
}
]
])
}
通过上面的flattened
函数,你会得到下面你想要创建的资源集合列表.
With the above flattened
function, you get below list of collection of resources, you would like to create.
flattened_value_output = [
{
"azs" = [
"1",
]
"cluster" = "yes"
"disable_password_authentication" = ""
"instance_type" = ""
"ip_index" = 0
"ip_key" = "main_hub"
"offer" = ""
"region" = "eastus"
"sku" = ""
"tags" = ""
"version" = ""
},
{
"azs" = [
"1",
]
"cluster" = "yes"
"disable_password_authentication" = ""
"instance_type" = ""
"ip_index" = 1
"ip_key" = "main_hub"
"offer" = ""
"region" = "eastus"
"sku" = ""
"tags" = ""
"version" = ""
},
{
"azs" = [
"1",
"2",
]
"cluster" = "yes"
"disable_password_authentication" = ""
"instance_type" = ""
"ip_index" = 0
"ip_key" = "spoke"
"offer" = ""
"region" = "eastus"
"sku" = ""
"tags" = ""
"version" = ""
},
{
"azs" = [
"1",
"2",
]
"cluster" = "yes"
"disable_password_authentication" = ""
"instance_type" = ""
"ip_index" = 1
"ip_key" = "spoke"
"offer" = ""
"region" = "eastus"
"sku" = ""
"tags" = ""
"version" = ""
},
{
"azs" = [
"1",
"2",
]
"cluster" = "yes"
"disable_password_authentication" = ""
"instance_type" = ""
"ip_index" = 2
"ip_key" = "spoke"
"offer" = ""
"region" = "eastus"
"sku" = ""
"tags" = ""
"version" = ""
},
{
"azs" = [
"1",
"2",
]
"cluster" = "yes"
"disable_password_authentication" = ""
"instance_type" = ""
"ip_index" = 3
"ip_key" = "spoke"
"offer" = ""
"region" = "eastus"
"sku" = ""
"tags" = ""
"version" = ""
},
]
从上面的集合中,你可以迭代 &使用如下唯一键创建资源::
From the above collection, you could iterate & create resources with unique keys like below::
resource "some_resource" "example" {
for_each = {
# Generate a unique string identifier for each instance
for ip in local.flattened_value_output : format("%s-%02d", ip.ip_key, ip.ip_index + 1) => ip
}
}
这样可以保证资源的创建或更新,因为每个资源都使用唯一的密钥.
This way, the creation or updating of resources is guaranteed as each resource uses a unique key.
更多详情请参考这个讨论 我与 Hashicorp 人员进行了讨论.
For more details, refer this discussion I had with Hashicorp personnel.
这篇关于嵌套列表中的 Terraform 嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!