gpt4 book ai didi

azure - 如何使用 for_each 将值从一个模块列表传递到另一个模块

转载 作者:行者123 更新时间:2023-12-02 23:37:16 31 4
gpt4 key购买 nike

我有一个创建 vnet 和子网的模块(模块 vnet):

resource "azurerm_virtual_network" "xxx" {
count = length(var.vnets)
name = var.vnets[count.index].vnet_name
address_space = var.vnets[count.index].address_space
location = var.resource_location
resource_group_name = var.resource_group_name

dynamic "subnet" {
for_each = var.vnets[count.index].subnets

content {
name = subnet.value.name
address_prefix = subnet.value.address
security_group = azurerm_network_security_group.xxx.id
}
}

tags = var.tags

}

var.vnets 是

vnets = [
{
vnet_name = "01-vnet"
address_space = ["10.0.0.0/23"]
subnets = [
{
name = "workloads-01"
address = "10.0.0.0/24"
}
]
},
{
vnet_name = "02-vnet"
address_space = ["10.0.2.0/23"]
subnets = [
{
name = "workloads-02"
address = "10.0.2.0/24"
}
]
}
]

然后我将子网传递给我的根文件

output "subnets" {
value = flatten(azurerm_virtual_network.xxx[*].subnet[*].id)

}

结果输出

Outputs:

subnets = [
"/subscriptions/XXX/resourceGroups/EIS/providers/Microsoft.Network/virtualNetworks/eis-01-vnet/subnets/eis-workloads-01",
"/subscriptions/XXX/resourceGroups/EIS/providers/Microsoft.Network/virtualNetworks/eis-02-vnet/subnets/eis-workloads-02",
]

现在我想在另一个模块(vm 模块)中创建虚拟机时使用这些子网:

resource "azurerm_network_interface" "nics" {
for_each = var.vms
name = "${each.value.name}-nic"
location = var.resource_location
resource_group_name = var.resource_group_name

ip_configuration {
name = "iface-${each.value.name}"
subnet_id = element(var.subnet_id, count.index)
private_ip_address_allocation = "Dynamic"
}

tags = var.tags

}

var.vms 在哪里

vms = {
vm1 = {
name = "vm1"
vm_size = "Standard_B1ms"
username = "centos"
password = "222!"
disk_size = "30"
sku = "7_9"
ext_ip = "1"
},
vm2 = {
name = "vm2"
vm_size = "Standard_B1ls"
username = "centos"
password = "111!"
disk_size = "35"
sku = "7_9"
ext_ip = ""
}
}

而 var.subnet_id 只是在 vm 模块中定义的。

variable "subnet_id" {}

我的根目录的 main.tf

module "vm" {
source = "./modules/vm"
for_each = var.vms

vms = var.vms
publicip_name = var.publicip_name
subnet_id = module.vnet.subnets[*]
resource_group_name = azurerm_resource_group.xxx.name
resource_location = azurerm_resource_group.xxx.location

tags = local.common_tags
}

如何将subnet_ids从模块vnet传递到模块vm,以便资源“azurerm_network_interface”将为vms中定义的每个键使用一个subnet_ids?

我将使用“element(var.subnet_id, count.index)”进行计数,但如何传递将使用 for_each 创建的资源的这些子网 ID?

最佳答案

您可以使用索引迭代 var.vms,如下所示:

resource "azurerm_network_interface" "nics" {
for_each = {for idx, vm in keys(var.vms): idx => var.vms[vm]}
name = "${each.value.name}-nic"
location = var.resource_location
resource_group_name = var.resource_group_name

ip_configuration {
name = "eis-iface-${each.value.name}"
subnet_id = element(var.subnet_id, each.key)
private_ip_address_allocation = "Dynamic"
}

tags = var.tags

}

注意:keys 按字母顺序返回 map 键(如果符合)。

关于azure - 如何使用 for_each 将值从一个模块列表传递到另一个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69240403/

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