我正在尝试创建一个Azure VMr模块及其所有其他属性

nfg76nw0  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(96)

尝试在VM扩展资源中获取VM ID,并且VM是使用for每个循环创建的。请在下面找到代码

resource "azurerm_windows_virtual_machine" "windowsvm" {
  for_each = var.virtual_machines
  name                = each.value.name
  resource_group_name = data.azurerm_resource_group.LBS_rg.name
  location            = data.azurerm_resource_group.LBS_rg.location
  size                = each.value.size
  admin_username      = each.value.admin_username
  admin_password      = each.value.admin_password

resource "azurerm_virtual_machine_extension" "vm-extensions" {
  for_each = var.vm-ext
  name                 = each.value.name
  virtual_machine_id   = azurerm_windows_virtual_machine.windowsvm[each.value.virtual_machines_name].id
  publisher            =  each.value.publisher        #Microsoft.Compute
  type                 = each.value.type              #CustomScriptExtension
  type_handler_version =  each.value.type_handler_version   #1.10

字符串
现在,当我在其他资源中创建的VM的ID是VM扩展代码时,它会给我以下错误:-

virtual_machine_id   = azurerm_windows_virtual_machine.windowsvm[each.value.virtual_machines_name].id

each.value is object with 4 attributes

 This object does not have an attribute named "virtual_machines_name"

3vpjnl9f

3vpjnl9f1#

我尝试创建一个Azure VM模块,并使用它的所有其他属性,我能够成功地配置需求。
错误消息表明,您在azurerm_virtual_machine_extension资源中迭代的对象没有名为virtual_machines_name的属性。这意味着您的each.value.virtual_machines_name没有正确引用VM名称。
为了克服这个问题,我尝试了一种配置,它可以很好地与预期的相同配置和代码更改一起工作。

我的地形配置:
main.tf:

provider "azurerm" {
    features {}
}

data "azurerm_resource_group" "existing_rg" {
  name = var.resource_group_name
}

# Virtual Network and Subnet Configuration
resource "azurerm_virtual_network" "main_vnet" {
  name                = "mainvk-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = var.location
  resource_group_name = data.azurerm_resource_group.existing_rg.name
}

resource "azurerm_subnet" "main_subnet" {
  name                 = "vkinternal"
  resource_group_name  = data.azurerm_resource_group.existing_rg.name
  virtual_network_name = azurerm_virtual_network.main_vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Network Interface Configuration
resource "azurerm_network_interface" "vm_nic" {
  for_each = var.virtual_machines

  name                = "${each.value.name}-nic"
  location            = var.location
  resource_group_name = data.azurerm_resource_group.existing_rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.main_subnet.id
    private_ip_address_allocation = "Dynamic"
    # public_ip_address_id if you need public IPs
  }
}

# Virtual Machine Configuration
resource "azurerm_windows_virtual_machine" "windowsvm" {
  for_each = var.virtual_machines

  name                = each.value.name
  resource_group_name = data.azurerm_resource_group.existing_rg.name
  location            = var.location
  size                = each.value.size
  admin_username      = each.value.admin_username
  admin_password      = each.value.admin_password

  network_interface_ids = [azurerm_network_interface.vm_nic[each.key].id]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
    # other required configurations for os_disk
  }

    source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"  # Adjust the SKU as per your requirement
    version   = "latest"
  }

  # other required configurations like OS profile, etc.
}

resource "azurerm_virtual_machine_extension" "vm_extensions" {
  for_each = var.vm_extensions

  name                 = each.value.name
  virtual_machine_id   = azurerm_windows_virtual_machine.windowsvm[each.value.virtual_machine_name].id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.10"

  settings = jsonencode({
    "fileUris"         = ["SAS_URL"],  # Replace with the actual URL of your script
    "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File powershell.ps1"
  })
}

字符串
请确保在容器blob中上传PowerShell脚本以及该PowerShell脚本的SAS_URL的相关权限。

variables.tf:

variable "virtual_machines" {
  description = "Configuration for each virtual machine"
  type = map(object({
    name             = string
    size             = string
    admin_username   = string
    admin_password   = string
  }))
}

variable "vm_extensions" {
  description = "Configuration for VM extensions"
  type = map(object({
    name                  = string
    virtual_machine_name  = string
    publisher             = string
    type                  = string
    type_handler_version  = string
  }))
}

variable "resource_group_name" {
  description = "The name of the resource group"
  type        = string
}

variable "location" {
  description = "The location/region in which to create the resources"
  type        = string
}

terraform.tfvars:

virtual_machines = {
  "vm1" = {
    name             = "vk-vm1"
    size             = "Standard_DS1_v2"
    admin_username   = "adminuser"
    admin_password   = "AdminPassword123!"
  },
  "vm2" = {
    name             = "vk-vm2"
    size             = "Standard_DS1_v2"
    admin_username   = "adminuser"
    admin_password   = "AdminPassword123!"
  }
}

vm_extensions = {
  "extension1" = {
    name                  = "vk-extension1"
    virtual_machine_name  = "vm1"
    publisher             = "Microsoft.Compute"
    type                  = "CustomScriptExtension"
    type_handler_version  = "1.10"
  },
  "extension2" = {
    name                  = "vk-extension2"
    virtual_machine_name  = "vm2"
    publisher             = "Microsoft.Compute"
    type                  = "CustomScriptExtension"
    type_handler_version  = "1.10"
  }
}

resource_group_name = "sakavya"
location            = "westus2"

输出:


的数据



相关问题