在Azure中从共享镜像创建虚拟机时出现的问题

huangapple go评论62阅读模式
英文:

Problems in creating vm from shared image in Azure

问题

这是我用于从共享镜像库创建Windows VM的代码:

resource "azurerm_virtual_machine" "vm" {
  name                  = "myvm" 
  location              = "East US"
  resource_group_name   = "MyResGroup"
  vm_size               = "Standard_D4as_v5"

  network_interface_ids =  [azurerm_network_interface.nic.id]

  storage_image_reference {
    id = "/subscriptions/4a4.....673/resourceGroups/rg-vm-images/providers/Microsoft.Compute/galleries/vmsgallary/images/testvmimage"
  }

  storage_os_disk {
    name               = "my_os_storage_disk"
    caching            = "ReadWrite"
    create_option      = "FromImage"
    managed_disk_type  = "Standard_LRS"
  }

  os_profile {
    computer_name  = "myvm"
    admin_username = "azureuser"
    admin_password = "MyComplexPassword"
  }

  os_profile_windows_config {
  }
}

它会出现以下错误(大约在40/50分钟后):

VM 'xxx'的操作系统配置未在分配的时间内完成。但是,检测到VM客户代理正在运行。这表明操作系统尚未适当准备好用作VM映像(使用CreateOption = FromImage)。要解决此问题,请使用VHD如其原样,使用CreateOption = Attach,或者适当准备它以用作映像:

Windows操作系统的说明:https://azure.microsoft.com/documentation/articles/virtual-machines-windows-upload-image/
Linux操作系统的说明:https://azure.microsoft.com/documentation/articles/virtual-machines-linux-capture-image/
删除并重新创建虚拟机可能会解决此问题。

以下是共享镜像的信息:
在Azure中从共享镜像创建虚拟机时出现的问题

操作系统类型:Windows
操作系统状态:通用化

在Azure中从共享镜像创建虚拟机时出现的问题

请注意,我已成功使用相同的代码创建Linux VM(除了仅适用于Linux的部分)。

Terraform版本:1.3.9
azurerm提供程序版本:3.46.0

英文:

Cross posted in HashiCorp Forum: https://discuss.hashicorp.com/t/problems-in-creating-vm-from-shared-image/52437

Here is the code I am using for creating a Windows VM from Shared Image gallery:

resource "azurerm_virtual_machine" "vm" {
  name                  = "myvm" 
  location              = "East US"
  resource_group_name   = "MyResGroup"
  vm_size               = "Standard_D4as_v5"

  network_interface_ids =  [azurerm_network_interface.nic.id]

  storage_image_reference {
    id = "/subscriptions/4a4.....673/resourceGroups/rg-vm-images/providers/Microsoft.Compute/galleries/vmsgallary/images/testvmimage"
  }

  storage_os_disk {
    name               = "my_os_storage_disk"
    caching            = "ReadWrite"
    create_option      = "FromImage"
    managed_disk_type  = "Standard_LRS"
  }

  os_profile {
    computer_name  = "myvm"
    admin_username = "azureuser"
    admin_password = "MyComplexPassword"
  }

  os_profile_windows_config {
  }
}

It gives the following error (after 40/50 mins or so):

OS Provisioning for VM ‘xxx’ did not finish in the allotted time. However, the VM guest agent was detected running. This suggests the guest OS has not been properly prepared to be used as a VM image (with CreateOption=FromImage). To resolve this issue, either use the VHD as is with CreateOption=Attach or prepare it properly for use as an image:
    
    Instructions for Windows: https://azure.microsoft.com/documentation/articles/virtual-machines-windows-upload-image/
    Instructions for Linux: https://azure.microsoft.com/documentation/articles/virtual-machines-linux-capture-image/
    Deleting and recreating the virtual machine may resolve the issue.

Here is the information of the shared image:
在Azure中从共享镜像创建虚拟机时出现的问题

OS type: Windows
OS state: Generalized

在Azure中从共享镜像创建虚拟机时出现的问题

Note that I have been successful in creating a Linux VM using the same code (except those that are only applicable to Linux).

terraform version: 1.3.9
azurerm provider: 3.46.0

答案1

得分: 0

以下是代码的翻译部分:

Check the following where I can create windows VM successfully:

I have used  `resource "azurerm_windows_virtual_machine" `

resource "azurerm_shared_image_gallery" "example" {
  name                = "exampleimagegallery"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = "eastus"
  description         = "Shared images and things."

  tags = {
    Hello = "There"
    World = "Example"
  }
}

resource "azurerm_shared_image" "example" {
  name                = "kavyamyimage"
  gallery_name        = azurerm_shared_image_gallery.example.name
  resource_group_name = data.azurerm_resource_group.example.name
  location            = "eastus"
  os_type             = "Windows"

  identifier {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
  }


data "azurerm_shared_image" "existing" {
  name                = "kavyamyimage"
  gallery_name        = azurerm_shared_image_gallery.example.name
  resource_group_name = data.azurerm_resource_group.example.name
}

output "sharedimageid" {
  value = data.azurerm_shared_image.existing.id
}
resource "azurerm_network_interface" "example" {
  name                = "kaexample-nic"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  ip_configuration {
    name                          = "example-ip-config"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_network" "example" {
  name                = "kaexample-virtual-network"
  address_space       = ["10.0.0.0/16"]
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "kkexample-subnet"
  address_prefixes     = ["10.0.0.0/24"]
  virtual_network_name = azurerm_virtual_network.example.name
  resource_group_name  = data.azurerm_resource_group.example.name
}

First I executed above with terraform apply and then deployed as below, as the vm required shared image to be created prior and if they are parallelly trying to be created and the vm depends on image creation , there will be conflicts.

Else, add depends _on parameter to the resource block such that VM that depends on image creation first .

resource "azurerm_windows_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = "eastus"
  size                = "Standard_F2"
  admin_username      = "adminuser"
  admin_password      = "P@$$w0rd1234!"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
  //source_image_id = data.azurerm_shared_image.existing.id
 depends_on = [
    data.azurerm_shared_image.existing
  ]

}

[![enter image description here][1]][1]

[![enter image description here][2]][2]

**Reference:** [azurerm_virtual_machine | terraform registry][3]

[1]: https://i.stack.imgur.com/9mCxu.png
[2]: https://i.stack.imgur.com/Y2G0D.png
[3]: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine#os_profile_windows_config

希望这对您有所帮助。如果您有任何其他问题,请随时提问。

英文:

Check the following where I can create windows VM successfully:

I have used resource "azurerm_windows_virtual_machine"

resource "azurerm_shared_image_gallery" "example" {
name                = "exampleimagegallery"
resource_group_name = data.azurerm_resource_group.example.name
location            = "eastus"
description         = "Shared images and things."
tags = {
Hello = "There"
World = "Example"
}
}
resource "azurerm_shared_image" "example" {
name                = "kavyamyimage"
gallery_name        = azurerm_shared_image_gallery.example.name
resource_group_name = data.azurerm_resource_group.example.name
location            = "eastus"
os_type             = "Windows"
identifier {
publisher = "MicrosoftWindowsServer"
offer     = "WindowsServer"
sku       = "2016-Datacenter"
}
data "azurerm_shared_image" "existing" {
name                = "kavyamyimage"
gallery_name        = azurerm_shared_image_gallery.example.name
resource_group_name = data.azurerm_resource_group.example.name
}
output "sharedimageid" {
value = data.azurerm_shared_image.existing.id
}
resource "azurerm_network_interface" "example" {
name                = "kaexample-nic"
location            = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
ip_configuration {
name                          = "example-ip-config"
subnet_id                     = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_network" "example" {
name                = "kaexample-virtual-network"
address_space       = ["10.0.0.0/16"]
location            = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name                 = "kkexample-subnet"
address_prefixes     = ["10.0.0.0/24"]
virtual_network_name = azurerm_virtual_network.example.name
resource_group_name  = data.azurerm_resource_group.example.name
}
First I executed above with terraform apply and then deployed as below, as the vm required shared image to be created prior and if they are parallelly trying to be created and the vm depends on image creation , there will be conflicts.
Else, add depends _on parameter to the resource block such that VM that depends on image creation first .
resource "azurerm_windows_virtual_machine" "example" {
name                = "example-machine"
resource_group_name = data.azurerm_resource_group.example.name
location            = "eastus"
size                = "Standard_F2"
admin_username      = "adminuser"
admin_password      = "P@$$w0rd1234!"
network_interface_ids = [
azurerm_network_interface.example.id,
]
os_disk {
caching              = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer     = "WindowsServer"
sku       = "2016-Datacenter"
version   = "latest"
}
//source_image_id = data.azurerm_shared_image.existing.id
depends_on = [
data.azurerm_shared_image.existing
]
}

在Azure中从共享镜像创建虚拟机时出现的问题

在Azure中从共享镜像创建虚拟机时出现的问题

Reference: azurerm_virtual_machine | terraform registry

huangapple
  • 本文由 发表于 2023年4月7日 05:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定