【问题标题】:Cannot attach an existing OS disk if the VM is created from a platform, user or a shared gallery image如果 VM 是从平台、用户或共享库映像创建的,则无法附加现有 OS 磁盘
【发布时间】:2021-06-08 22:31:11
【问题描述】:

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

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

【问题讨论】:

    标签: terraform virtual-machine snapshot terraform-provider-azure azure-managed-disk


    【解决方案1】:

    如果您在storage_os_disk 中指定create_option 至Attach,则意味着它将从专用VM 映像创建VM。所以我们不需要在这里指定os_profile和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。

    验证结果。

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2020-10-10
      相关资源
      最近更新 更多