【问题标题】:Getting error PropertyChangeNotAllowed while creating VM in Azure在 Azure 中创建 VM 时出现错误 PropertyChangeNotAllowed
【发布时间】:2021-11-27 12:47:12
【问题描述】:

我正在尝试使用以下配置在 Azure 中创建 VM。

resource “azurerm_virtual_machine” “VM38” {
    name = “VM38”
    resource_group_name = data.azurerm_resource_group.myRG.name
    location = data.azurerm_resource_group.myRG.location
    vm_size = “Standard_F16s_v2”
    delete_os_disk_on_termination = true
    delete_data_disks_on_termination = true
    
    os_profile {
        computer_name = “vm38”
        admin_username = “adminuser”
        admin_password = “Password1234!”
        custom_data = base64encode(data.cloudinit_config.hybrid_vm38_cloudinit_cfg.rendered)
    }
    
    os_profile_linux_config {
        disable_password_authentication = false
    }
    
    storage_image_reference {
        id = data.azurerm_image.my_image.id
    }
    
    depends_on = [aws_instance.vm12]
    
    storage_os_disk {
        name = “VMDisk”
        create_option = “FromImage”
        caching = “ReadWrite”
        #disk_size_gb = 16
        #os_type = “Linux”
        #managed_disk_type = “Standard_LRS”
        vhd_uri = var.vmVHDURI
    }
    
    network_interface_ids = [azurerm_network_interface.mgmtNwIntf.id, azurerm_network_interface.transportNwIntf.id]
}

当我执行 terraform apply 时,我遇到了错误……

错误:compute.VirtualMachinesClient#CreateOrUpdate:发送请求失败:StatusCode=0 – 原始错误:autorest/azure:服务返回错误。 Status= Code=“PropertyChangeNotAllowed” Message=“不允许更改属性‘osDisk.name’。”目标=“osDisk.name”

使用 azurerm_virtual_machine.VM38, 在 az_virtual_machine.tf 第 1 行,资源“azurerm_virtual_machine”“VM38”中: 1:资源“azurerm_virtual_machine”“VM38”{

请告诉我如何解决此问题。

Terraform 和 Azure 提供程序版本详细信息如下: Terraform v1.0.8 在 linux_amd64 上

  • 提供者 registry.terraform.io/hashicorp/azurerm v2.79.1

感谢和问候, -拉维

**In terraform.tfvars**
resourceGroupName   = "myResourceGroup"
deviceImageName     = "myDeviceImageName"

**In cloudinit_config.tf**
data "cloudinit_config" "hybrid_vm38_cloudinit_cfg" {
    gzip = false
    base64_encode = false
    depends_on = [aws_instance.hybrid_vm12]
    part {
        filename = "cloud-config"
        content_type = "text/cloud-config"
        content = file("cloudinit/vm38_cloud_config.yaml")
    }
    part {
        filename = "config-C8K.txt"
        content_type = "text/cloud-boothook"
        content = file("cloudinit/vm38_cloud_boothook.cfg")
    }
}

**In az_resource_group.tf**
data "azurerm_resource_group" "vm38RG" {
  name = var.resourceGroupName
}

**In az_image.tf**
data "azurerm_image" "deviceImage" {
  name = var.deviceImageName
  resource_group_name = data.azurerm_resource_group.vm38RG.name
}

**In az_virtual_network.tf**
resource "azurerm_virtual_network" "vm38VirtualNw" {
    name                = "vm38VirtualNw"
    address_space       = ["30.0.0.0/16"]
    location            = "eastus"
    resource_group_name = data.azurerm_resource_group.vm38RG.name

    tags = {
        environment = "My virtual network"
    }
}

**In az_subnet.tf**
resource "azurerm_subnet" "vm38MgmtSubnet" {
  name                 = "vm38MgmtSubnet"
  resource_group_name  = data.azurerm_resource_group.vm38RG.name
  virtual_network_name = azurerm_virtual_network.vm38VirtualNw.name
  address_prefixes     = ["30.0.11.0/24"]
}

resource "azurerm_subnet" "vm38TransportSubnet" {
  name                 = "vm38TransportSubnet"
  resource_group_name  = data.azurerm_resource_group.vm38RG.name
  virtual_network_name = azurerm_virtual_network.vm38VirtualNw.name
  address_prefixes     = ["30.0.12.0/24"]
}

**In az_network_interface.tf**
resource "azurerm_network_interface" "vm38MgmtNwIntf" {
  name                 = "vm38MgmtNwIntf"
  location             = data.azurerm_resource_group.vm38RG.location
  resource_group_name  = data.azurerm_resource_group.vm38RG.name
  ip_configuration {
    name                          = "vm38MgmtPvtIP"
    subnet_id                     = azurerm_subnet.vm38MgmtSubnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id = azurerm_public_ip.vm38MgmtPublicIP.id
  }
}

resource "azurerm_network_interface" "vm38TransportNwIntf" {
  name                 = "vm38TransportNwIntf"
  location             = data.azurerm_resource_group.vm38RG.location
  resource_group_name  = data.azurerm_resource_group.vm38RG.name

  ip_configuration {
    name                          = "vm38TransportPvtIP"
    subnet_id                     = azurerm_subnet.vm38TransportSubnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id = azurerm_public_ip.vm38TransportPublicIP.id
  }
}

**In az_virtual_machine.tf**
resource "azurerm_virtual_machine" "VM38" {
  name                = "VM38"
  resource_group_name = data.azurerm_resource_group.vm38RG.name
  location            = data.azurerm_resource_group.vm38RG.location
  vm_size             = "Standard_F16s_v2"
  delete_os_disk_on_termination = true
  #delete_data_disks_on_termination = true

  os_profile {
    computer_name   = "vm38"
    admin_username  = "adminuser"
    admin_password  = "Password1234!"
    custom_data     = base64encode(data.cloudinit_config.hybrid_vm38_cloudinit_cfg.rendered)
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  storage_image_reference {
    id = data.azurerm_image.deviceImage.id
  }

  depends_on = [aws_instance.hybrid_vm12]
  
  storage_os_disk {
    name = "osDisk"
    create_option = "FromImage"
    caching              = "ReadWrite"
    #disk_size_gb = 16
    #os_type = "Linux"
    managed_disk_type = "Standard_LRS"
  }

  /*
  storage_data_disk {
    name = "vm38SecondaryDisk"
    caching = "ReadWrite"
    create_option = "Empty"
    disk_size_gb = 2048
    lun = 0
    managed_disk_type = "Premium_LRS"
  }
  */

  network_interface_ids = [
    azurerm_network_interface.vm38MgmtNwIntf.id,
    azurerm_network_interface.vm38TransportNwIntf.id
  ]
}

【问题讨论】:

  • 你好@ravindra,我们不能在从镜像创建VM时更改Os_disk名称,但是我们可以先从镜像创建一个托管磁盘,然后在创建时将其附加到VM..将测试把它放在我的环境中,让你知道
  • 我可以知道你是否有创建图像的 os_disk 吗?
  • 是的,我的操作系统映像磁盘存在于 Azure“映像”中。
  • 不,我的意思是创建映像的虚拟机的原始 os_disk 是在创建映像后存在还是删除
  • 你好@ravindra,为 2 个场景添加了解决方案。请让我知道这可不可以帮你 。如果有帮助,请Accept it as an Answer,以便遇到相同问题的其他人可以找到此解决方案并解决他们的问题。

标签: azure terraform terraform-provider-azure azure-rm


【解决方案1】:

创建 VM 时不能更改 os_disk 名称。它应该是 "osdisk" 或以它开头的东西。

我使用以下代码进行了测试:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "ansuman-resources"
  location = "West US 2"
}

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

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = "${azurerm_resource_group.example.name}"
  virtual_network_name = "${azurerm_virtual_network.example.name}"
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "ansuman-nic"
  location            = "${azurerm_resource_group.example.location}"
  resource_group_name = "${azurerm_resource_group.example.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${azurerm_subnet.example.id}"
    private_ip_address_allocation = "Dynamic"
  }
}

# we assume that this Custom Image already exists
data "azurerm_image" "custom" {
  name                = "ansumantestvm-image-20211007225625"
  resource_group_name = "resourcegroup"
}

resource "azurerm_virtual_machine" "example" {
  name                  = "ansuman-vm"
  location              = "${azurerm_resource_group.example.location}"
  resource_group_name   = "${azurerm_resource_group.example.name}"
  network_interface_ids = ["${azurerm_network_interface.example.id}"]
  vm_size               = "Standard_F2"

  # This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
  # NOTE: This may not be optimal in all cases.
  delete_os_disk_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.custom.id}"
  }

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

  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_windows_config {
  }
}

输出:

注意:请确保在从原始VM创建映像时,首先对其进行泛化。如果它没有被通用化,那么从自定义镜像创建的虚拟机将卡在创建状态并且无法启动。


如果您想将 osdisk 名称更改为您选择的名称,那么作为一种解决方案,尝试首先使用 create option "copy" or "import" 从映像创建托管 os 磁盘,然后在创建 VM 时附加磁盘,就像从自定义映像创建托管磁盘一样也不支持,只能为平台图像或市场图像完成。你可以参考这个GitHub issue和这个Github issue

参考 terraform 代码以解决类似问题,以便为从平台图像/市场图像创建的 osdisk 提供自定义名称,Charles Xu 已在此 SO thread 中完成。

【讨论】:

  • 我尝试按照您的示例启动虚拟机,并对我的初始配置进行了一些更改。但是,我仍然遇到同样的错误。
  • @Ravindra,我可以知道您正在尝试哪种解决方案吗? 1个还是2个?
  • 我刚刚在您的上述示例中引用了示例 terraform 配置并更新了我的配置。有了这个,我仍然看到同样的错误。
  • 您能否在问题中添加您正在使用的更新代码。请同时包含变量
  • 让讨论转移到聊天chat.stackoverflow.com/rooms/238019/…
猜你喜欢
  • 2023-04-02
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2021-05-05
  • 2015-08-03
  • 2020-06-04
相关资源
最近更新 更多