gpt4 book ai didi

azure - 使用 for_each 创建不同数量的具有唯一 NIC 的 VM

转载 作者:行者123 更新时间:2023-12-05 06:55:19 24 4
gpt4 key购买 nike

我正在尝试创建不同数量的具有不同配置的虚拟机。我在 azurerm_windows_virtual_machine 资源上设置 for_each 并循环遍历 tfvars 文件中的映射集。该变量在模块中设置,但在 tfvars 文件中定义。

我希望能够创建 x 数量的虚拟机,每个虚拟机都附加一个唯一的 NIC。我能够创建虚拟机,但配置失败,因为 NIC 不是唯一的。我尝试使用相同的变量添加 for_each 但出现以下错误:

Error: Incorrect attribute value type

on ../modules/compute/windows_vm/windows_vm.tf line 96, in resource "azurerm_network_interface_application_security_group_association" "application_security_group_association":
96: network_interface_id = [azurerm_network_interface.network_interface[each.key].id]

Inappropriate value for attribute "network_interface_id": string required.


Error: Incorrect attribute value type

on ../modules/compute/windows_vm/windows_vm.tf line 96, in resource "azurerm_network_interface_application_security_group_association" "application_security_group_association":
96: network_interface_id = [azurerm_network_interface.network_interface[each.key].id]

Inappropriate value for attribute "network_interface_id": string required.

这是我的代码:

# VM Network Interface
resource "azurerm_network_interface" "network_interface" {
for_each = var.servers
name = "nic-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}${each.value.name}"
resource_group_name = var.resource_group
location = var.location
enable_ip_forwarding = "false"
enable_accelerated_networking = "false"

ip_configuration {
name = "ipconfig1"
subnet_id = data.azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
primary = "true"
}

}

# Application Security Group
resource "azurerm_application_security_group" "application_security_group" {
name = "asg-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}"
resource_group_name = var.resource_group
location = var.location
}

resource "azurerm_network_interface_application_security_group_association" "application_security_group_association" {
for_each = var.servers
network_interface_id = [azurerm_network_interface.network_interface[each.key].id]
application_security_group_id = azurerm_application_security_group.application_security_group.id

}

resource "azurerm_network_interface_security_group_association" "network_security_group_association" {
for_each = var.servers
network_interface_id = [azurerm_network_interface.network_interface[each.key].id]
network_security_group_id = azurerm_network_security_group.network_security_group.id
}

# Azure Virtual Machine
resource "azurerm_windows_virtual_machine" "virtual_machine" {
for_each = var.servers
name = "vm-${var.environment}-${var.vm_identifier}${each.value.name}"
location = var.location
resource_group_name = var.resource_group
zone = each.value.zone
size = var.vm_size
network_interface_ids = [azurerm_network_interface.network_interface[each.key].id]
computer_name = "${var.vm_identifier}${each.value.name}"
admin_username = xxxx
admin_password = xxxx
provision_vm_agent = "true"
source_image_id = data.azurerm_shared_image.shared_image.id


boot_diagnostics {
storage_account_uri = data.azurerm_storage_account.diag_storage_account.primary_blob_endpoint
}

os_disk {
name = "vm-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}-os${each.value.name}"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}

depends_on = [azurerm_network_interface.network_interface]
}

根模块内的变量,在 for_each 中使用并在模块变量.tf 中设置:

variable "servers" {
description = "Variable for defining each instance"
}

模块中映射到每个环境的每个 tfvar 的变量:

variable "desktop_servers" {
description = "Variable for defining each instance"
}

variable "db_servers" {
description = "Variable for defining each instance"
}

然后在 tfvar 中定义上述内容,如下所示:

desktop_servers = {
"Server_1" = {
name = 1,
zone = 1
}
"Server_2" = {
name = 2,
zone = 2
}
"Server_3" = {
name = 3,
zone = 3
}
}

db_servers = {
"Server_1" = {
name = 1,
zone = 1
}
}

最佳答案

您正在将一个列表分配给network_interface_id,但它应该只是一个字符串。

因此,而不是:

network_interface_id          = [azurerm_network_interface.network_interface[each.key].id]

应该是

network_interface_id          = azurerm_network_interface.network_interface[each.key].id

关于azure - 使用 for_each 创建不同数量的具有唯一 NIC 的 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65412026/

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