使用 Terraform 创建多个 Azure VM 导致 OS 磁盘出现问题

使用 Terraform 创建多个 Azure VM 导致 OS 磁盘出现问题

第一次在这里提问。

我正在尝试使用模块和计数函数在 terraform 中创建多个 azure VM。我似乎遇到的问题是,当创建第二台 VM 的 OS 磁盘时,它出现以下错误:

* azurerm_virtual_machine.vm.0: compute.VirtualMachinesClient#CreateOrUpdate:            
Failure sending request: StatusCode=0 -- Original Error: autorest/azure:     
Service  returned an error. Status=<nil> Code="PropertyChangeNotAllowed" 
Message="Changing   property 'osDisk.name' is not allowed."   
Target="osDisk.name"

这几乎就像是试图使用与第一个虚拟机的操作系统磁盘相同的名称。有人知道我该如何解决这个问题吗?

这是我的代码:

resource "azurerm_network_interface" "vm_nic" {
  name                      = "${var.hostname}-nic"
  location                  = "${var.location}"
  resource_group_name       = "${var.rg_name}"
  network_security_group_id = "${var.nsg_id}"
  count                     = "${var.vm_count}"

  ip_configuration {
    name                          = "${var.hostname}-ipconfig"
    subnet_id                     = "${var.subnet_id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = "${azurerm_public_ip.vm_pip.*.id[count.index]}"
  }
}

resource "azurerm_public_ip" "vm_pip" {
  name                         = "${var.hostname}-pip-${random_id.namegen.hex}"
  location                     = "${var.location}"
  resource_group_name          = "${var.rg_name}"
  public_ip_address_allocation = "dynamic"
  count                        = "${var.vm_count}"
}

resource "azurerm_managed_disk" "managed_disk_data" {
  name                 = "${var.hostname}-${count.index + 1}-datadisk"
  location             = "${var.location}"
  resource_group_name  = "${var.rg_name}"
  storage_account_type = "Premium_LRS"
  create_option        = "Empty"
  disk_size_gb         = "${var.data_disk_size}"
  count                = "${var.vm_count}"
}

resource "azurerm_virtual_machine" "vm" {
  name                  = "${var.hostname}-${count.index + 1}"
  location              = "${var.location}"
  resource_group_name   = "${var.rg_name}"
  network_interface_ids = ["${azurerm_network_interface.vm_nic.*.id[count.index]}"]
  vm_size               = "${var.vm_size}"
  depends_on            = ["azurerm_public_ip.vm_pip"]
  count                 = "${var.vm_count}"

  # Uncomment this line to delete the OS disk automatically when deleting the VM
  delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "/subscriptions/xxxx/resourceGroups/rg-shared/providers/Microsoft.Compute/images/xx-image-xx"
  }

  storage_os_disk {
    name              = "myosdisk1${count.index}"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }

  # Optional data disks

  storage_data_disk {
    name            = "${azurerm_managed_disk.managed_disk_data.*.name[count.index]}"
    managed_disk_id = "${azurerm_managed_disk.managed_disk_data.*.id[count.index]}"
    create_option   = "Attach"
    lun             = 0
    disk_size_gb    = "${azurerm_managed_disk.managed_disk_data.*.disk_size_gb[count.index]}"
  }

非常感谢

答案1

你想要的是这样的:

resource "azurerm_virtual_machine" "server" {
  name                  = "server-${count.index}"
  count                 = "${var.instance_count}"
  storage_os_disk {
    name              = "server-${count.index}-os"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
}

…但看起来你已经在这么做了……

* azurerm_virtual_machine.vm.0: compute.VirtualMachinesClient#CreateOrUpdate:            
Failure sending request: StatusCode=0 -- Original Error: autorest/azure:     
Service  returned an error. Status=<nil> Code="PropertyChangeNotAllowed" 
Message="Changing   property 'osDisk.name' is not allowed."   
Target="osDisk.name"

.0表明这是您的第一台虚拟机上的错误。

你是先申请了,然后把磁盘名称周围的代码改一下,然后再申请试试?先把旧资源删掉。(感谢@QuentinMoss)

编辑:这个地形问题可能适用于您:https://github.com/terraform-providers/terraform-provider-azurerm/issues/956

相关内容