在Terraform中为每个Azure虚拟机引用NIC ID

sdnqo3pr  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(83)

我从我的代码中得到了这个片段:
这将创建number_of_resources中定义的3个NIC

resource "azurerm_network_interface" "nic" {
  count = var.number_of_resources
  name  = "nic-${element(var.resourcese_name, count.index)}"
    
  location            = var.location
  resource_group_name = var.rg
    
  ip_configuration {
    subnet_id = azurerm_virtual_network.main_net.*.subnet[0].id
  }
}

字符串
VM块,这将创建3个VM:

resource "azurerm_windows_virtual_machine" "VMs"{
  count = var.number_of_resources
    
  name = "VM-${element(var.resources_name, count.index)}"
  resource_group_name = var.rg
    
  location = var.location
    
  network_interface_ids = [?]
}


每个VM都需要将其自己的网络接口卡(NIC)连接到同一子网。我已经定义了创建VM及其各自NIC所需的变量和资源。
但是,当我尝试使用ID将每个VM与其相应的NIC关联时,我遇到了一个问题。
尝试将nsg与NIC关联时出现相同问题:

resource "azurerm_network_interface_security_group_association" "example" {
   count = var.number_of_resources
    
   network_interface_id      = azurerm_network_interface.nic[count.index + 1].id
   network_security_group_id = azurerm_network_security_group.main-nsg[count.index + 1].id
}


网址:Variable.tf

variable "rg" {
  description = "The default resource group for this proj"
  type = string   
  default = "rg_ADLab"
}
    
variable "location"{
  description = "the default location for this proj"
  type = string
  
  default = "East US"
}

variable "resources_name" {
  type = list(string)
  default = [ "DC","Client1", "Client2" ]
}

variable "number_of_resources"{
  type = number
 
  default = 3
}

oxf4rvwz

oxf4rvwz1#

根据问题中提供的代码,这应该可以工作:

resource "azurerm_windows_virtual_machine" "VMs"{
  count = var.number_of_resources
  name = "VM-${element(var.resources_name, count.index)}"
  resource_group_name = var.rg
  location = var.location

  network_interface_ids = [azurerm_network_interface.nic[count.index].id]
}

字符串

相关问题