我正在尝试在所有已定义的resource_groups中创建所有已定义的virtual_networks。
变量如下所示:
inputs = {
virtual_networks = {
"test-net1": {
address_space = ["10.0.0.0/24"]
}
"test-net2": {
address_space = ["10.0.1.0/24"]
}
}
resource_groups = {
"group1": {
location = "West Europe"
}
"group2": {
location = "East Europe"
}
}
}
资源块如下所示:
resource "azurerm_virtual_network" "virtual_network" {
for_each = var.resource_groups
name = <FIRST KEY OF var.virtual_networks>
resource_group_name = each.key
location = each.value["location"]
address_space = <ADDRESS_SPACE VALUE OF FIRST NETWORK FROM var.virtual_networks>
}
我尝试了许多不同的方法来转换/重组/合并变量的Map,这样我就可以用更好的方式来迭代它们,但是我就是找不到正确的结构和如何构建它。
我建立了下面的局部变量来帮助解决这个问题。但是我不能用一种方式来构造Map,这样我就可以在一个单个资源块中循环它。
locals = {
resource_groups = {
for key, resource_groups in var.resource_groups : "${key}" =>
merge(
var.virtual_networks,
resource_groups
)
}
}
输出:
group1 = {
location = "West Europe"
test-net1 = {
address_space = [
"10.0.0.0/24",
]
}
test-net2 = {
address_space = [
"10.0.1.0/24",
]
}
}
group2 = {
location = "West Europe"
test-net1 = {
address_space = [
"10.0.0.0/24",
]
}
test-net2 = {
address_space = [
"10.0.1.0/24",
]
}
}
1条答案
按热度按时间piztneat1#
据我所知,你想在两个字典上创建一个嵌套循环,你可以像这样构造你的局部:
产出将导致:
您的资源将如下所示: