如果 VM 是从平台、用户或共享库图像创建的,则无法附加现有的 OS 磁盘

Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image

创建 Azure 虚拟机并附加托管磁盘(从快照创建)

获取错误,

“如果 VM 是从平台、用户或共享图库图像创建的,则无法附加现有 OS 磁盘

尝试将“创建选项”调整为“复制”、“导入”等,但 none 成功了

代码如下,

        provider "azurerm" {
    subscription_id = "abcd"
    features {}
}

resource "azurerm_managed_disk" "example" {
  name                 = "managed-disk-01"
  location             = "East US"
  resource_group_name  = "RG-APP"
  storage_account_type = "Premium_LRS"
  create_option        = "Copy"
  disk_size_gb         = "127"
  source_resource_id   = azurerm_snapshot.example.id
  os_type              = "Windows"
}

resource "azurerm_snapshot" "example" {
  name                = "snapshot"
  location            = "East US"
  resource_group_name = "RG-APP"
  create_option       = "Copy"
  source_uri          = "/subscriptions/abcv/resourceGroups/RG-APP/providers/Microsoft.Compute/disk/AppA_OsDisk_1_0e"
}

data "azurerm_subnet" "subnet" {
   name = "vnet-sn-app"
   resource_group_name = "rg-network"
   virtual_network_name = "vnet"
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = "East US"
  resource_group_name = "RG-APP"

  ip_configuration {
    name                          = "ipconfig"
    subnet_id                     = "${data.azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_availability_set" "DemoAset" {
  name                = "example-aset"
  location            = "East US"
  resource_group_name = "RG-APP"
}
resource "azurerm_virtual_machine" "example" {
  name                = "example-machine-01"
  resource_group_name = "RG-APP"
  location            = "East US"
  vm_size                = "Standard_F2"
  availability_set_id = azurerm_availability_set.DemoAset.id
  delete_os_disk_on_termination = false
  delete_data_disks_on_termination = false
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_windows_config {
    provision_vm_agent ="true"
    }

  storage_os_disk {
    name              = "myosdisk1" 
    managed_disk_id   = "${azurerm_managed_disk.example.id}"
    caching           = "ReadWrite"
    create_option     = "Attach"
  }

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

请帮忙指导解决这个问题。谢谢!

如果您在storage_os_disk 中指定create_optionAttach,这意味着它将从专门的VM 映像创建VM。所以我们不需要在这里指定os_profilestorage_image_reference

您可以这样更改模板:

resource "azurerm_virtual_machine" "example" {
  name                = "example-machine-01"
  resource_group_name = "RG-APP"
  location            = "East US"
  vm_size             = "Standard_F2"  # this matches the old VM size
  availability_set_id              = azurerm_availability_set.DemoAset.id
  delete_os_disk_on_termination    = false
  delete_data_disks_on_termination = false
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]
#   os_profile {
#     computer_name  = "hostname"
#     admin_username = "testadmin"
#     admin_password = "Password1234!"
#   }
  os_profile_windows_config {
    provision_vm_agent = "true"
  }

  storage_os_disk {
    name            = "managed-disk-01"  # this matches new created managed disk name
    managed_disk_id = azurerm_managed_disk.example.id
    caching         = "ReadWrite"
    create_option   = "Attach"
    os_type         = "Windows"    # add this os_type
  }

#   storage_image_reference {
#     publisher = "MicrosoftWindowsServer"
#     offer     = "WindowsServer"
#     sku       = "2016-Datacenter"
#     version   = "latest"
#   }
}

这将从该快照创建一个新的 Azure VM,您可以使用现有 OS 磁盘中的旧用户名和密码登录到新 VM。

验证结果。