尝试创建 2 个附加 NIC 的虚拟机时出现 Terraform 错误

问题描述 投票:0回答:1

我正在尝试创建两个网卡以连接到两个虚拟机。 NIC 已创建,但现在我正在尝试创建 VM 并使用 NIC,但我收到以下 Terraform 错误

resource "azurerm_linux_virtual_machine" "web_linuxvm" {
  count = var.vm-count

  name = "linuxvm-${count.index}"
  resource_group_name = var.location
  location = var.resource_group_name
  size = "Standard_DS1_v2"
  admin_username = "azureuser"
  network_interface_ids = format("nic-%s",var.nics[count.index])
  admin_ssh_key {
    username = "azureuser"
    public_key = file("${path.module}/ssh-keys/vm-ssh.pub")
  }
  os_disk {
    caching = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
  source_image_reference {
    publisher = "RedHat"
    offer = "RHEL"
    sku = "83-gen2"
    version = "latest"
  }
    
  custom_data = base64encode(local.vm_custom_data)  
  /*
  can do custom_data = filebase64("${path.module}/from-script.sh")
  would be better 
  */
}

resource "azurerm_network_interface" "network-interface" {
  count = var.vm-count

  name                = "nic-${count.index}"
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "vm-${count.index}"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}



module "compute" {
  source = "../module/compute"
  vm-count = var.vm-count
  nics = ["nic0","nic1"]
  resource_group_name = azurerm_resource_group.rg.name
  location            = var.region
}

计划失败。 Terraform 在生成此计划时遇到错误。

╷ │

Error: Incorrect attribute value type
│ 
│   on ../module/compute/main.tf line 20, in resource "azurerm_linux_virtual_machine" "web_linuxvm":
│   20:   network_interface_ids = format("nic-%s",var.nics[count.index])
│     ├────────────────
│     │ count.index is 1
│     │ var.nics is tuple with 2 elements
│ 
│ Inappropriate value for attribute "network_interface_ids": list of string
│ required.
╵
╷
│ Error: Incorrect attribute value type
│ 
│   on ../module/compute/main.tf line 20, in resource "azurerm_linux_virtual_machine" "web_linuxvm":
│   20:   network_interface_ids = format("nic-%s",var.nics[count.index])
│     ├────────────────
│     │ count.index is 0
│     │ var.nics is tuple with 2 elements
│ 
│ Inappropriate value for attribute "network_interface_ids": list of string
│ required.
╵
Operation failed: failed running terraform plan (exit 1)
azure terraform terraform-provider-azure azure-virtual-network
1个回答
0
投票

错误消息指出

network_interface_ids
参数需要一个 list,但您只提供了一个字符串。

我特别不熟悉这种资源类型,但我猜如果您只想分配一个网络接口,那么您可以分配一个单元素列表:

  network_interface_ids = [format("nic-%s",var.nics[count.index])]

值周围的括号

[
]
是“元组构造函数”语法,因此这个结果实际上是一个单元素元组,但 Terraform 可以自动将其转换为列表,因此上面的表达式应该满足提供者模式的类型约束。

© www.soinside.com 2019 - 2024. All rights reserved.