0

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

得到错误,

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

尝试将“创建选项”中的调整为“复制”、“导入”等,但都没有成功

代码如下,

        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"
  }
}

请帮助指导解决此问题。谢谢!

4

1 回答 1

0

如果在 中指定create_optionto Attachstorage_os_disk则意味着它将从专门的 VM 映像创建 VM。所以我们不需要在这里指定os_profileand storage_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,您可以使用现有操作系统磁盘中的旧用户名和密码登录到新 VM。

验证结果。

在此处输入图像描述

于 2021-03-11T06:19:06.610 回答