gpt4 book ai didi

azure - 在azure中创建多个子网

转载 作者:行者123 更新时间:2023-12-03 02:20:14 25 4
gpt4 key购买 nike

<小时/>

变量.tf

variable "vnet" {
type = map(any)
description = "creating rg and vmet"
default = {
"rg1" = {
vnet_name = "vnet1"
address = ["10.0.0.0/16"]
subnet_name = ["snet1", "snet2"]
subnet_address = ["10.1.0.0/24", "10.2.0.0/24"]
location = "south india"
}
}
}
<小时/>

main.tf

resource "azurerm_subnet" "mysubnet" {
for_each = var.vnet
name = each.value["subnet_name"]
address_prefixes = each.value["subnet_address"]
address_prefixes = each.value["subnet_address"]
virtual_network_name = each.value["vnet_name"]
resource_group_name = each.key

}

错误:

Error: Incorrect attribute value type
on main.tf line 25, in resource "azurerm_subnet" "mysubnet":
name = each.value["subnet_name"]
each.value["subnet_name"] is tuple with 2 elements

属性“name”的值不合适:需要字符串。

如何迭代创建多个子网?

最佳答案

您必须首先展平变量。例如:

locals {
vnet_flat = merge([
for group_name, details in var.vnet:
{for idx in range(length(details.subnet_name)):
"${group_name}-${idx}" => {
group_name = group_name
vnet_name = details.vnet_name
address = details.address
subnet_name = details.subnet_name[idx]
subnet_address = details.subnet_address[idx]
location = details.location
}
}
]...)
}

resource "azurerm_subnet" "mysubnet" {
for_each = local.vnet_flat
name = each.value.subnet_name
address_prefixes = [each.value.subnet_address]
virtual_network_name = each.value.vnet_name
resource_group_name = each.value.group_name
}

... 适用于 Expanding Function Arguments .

关于azure - 在azure中创建多个子网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69375243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com