gpt4 book ai didi

azure - 分配给单个虚拟机的多个网络接口(interface)的 Terraform azure 输出

转载 作者:行者123 更新时间:2023-12-03 01:24:56 26 4
gpt4 key购买 nike

不确定以前是否有人遇到过这个问题,我有多个网络接口(interface)需要附加到 Azure 中的虚拟机,我将网络接口(interface)详细信息作为映射传递。我需要使用网络接口(interface)的输出并将其应用到虚拟机资源中的network_interface_ids。我当前分配network_interface_ids的代码似乎没有从azurerm_network_interface的输出中获取网络接口(interface)ID。这是我的代码的简短快照:

 resource "azurerm_network_interface" "vmNic" {
for_each = var.network_interfaces

name = each.value.name
resource_group_name = var.resource_group_name
location = var.resource_group_location

ip_configuration {
name = "${each.value.name}-ip"
subnet_id = each.value.subnet_id
private_ip_address_allocation = "Static"
private_ip_address = each.value.private_ip
}
}

resource "azurerm_virtual_machine" "vm" {
count = length(azurerm_network_interface.vmNic)
name = var.vm_name
location = var.resource_group_location
resource_group_name = var.resource_group_name
network_interface_ids = try(azurerm_network_interface.vmNic.*.id[count.index], null)

你知道我在分配network_interface_ids时做错了什么吗?

最佳答案

您似乎想要创建多个 NIC 并将它们附加到单个虚拟机,并且变量 network_interfaces 看起来像一个包含对象元素的列表。所以如果我是对的。您可以像这样更改代码:

resource "azurerm_network_interface" "vmNic" {
count = length(var.network_interfaces)

name = element(var.network_interfaces, count.index)["name"]
resource_group_name = var.resource_group_name
location = var.resource_group_location

ip_configuration {
name = "${element(var.network_interfaces, count.index)["name"]}-ip"
subnet_id = element(var.network_interfaces, count.index)["subnet_id"]
private_ip_address_allocation = "Static"
private_ip_address = element(var.network_interfaces, count.index)["private_ip"]
}
}

resource "azurerm_virtual_machine" "vm" {
count = length(azurerm_network_interface.vmNic)
name = var.vm_name
location = var.resource_group_location
resource_group_name = var.resource_group_name
network_interface_ids = azurerm_network_interface.vmNic.*.id

这里的变量network_interfaces看起来像这样:

variable "network_interfaces" {
default = [
{
name = "test1",
subnet_id = "xxxxxx",
private_ip = "x.x.x.x"
},
{
name = "test2",
subnet_id = "xxxxxx",
private_ip = "x.x.x.x"
}
]
}

关于azure - 分配给单个虚拟机的多个网络接口(interface)的 Terraform azure 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66828139/

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