Azure云上的Terraform-VM名称和数据磁盘名称未对齐

时间:2019-02-20 00:53:07

标签: azure terraform azure-virtual-machine

我已经创建了虚拟机并添加了数据磁盘,但是存在一个问题。虚拟机名称和数据磁盘名称不对齐。请参考下面的屏幕截图。

NXTPREPAOS-1 Machine Disks

NXTPREPAOS-1 Machine Disks

NXTPREPAOS-5 Machine Disk

terraform代码如下:

s = 'blah blah blah blah blah' \
    'blah blah blah blah blah blah blah foo bar puke broke joke enchanted kingdom' \
    'digital divide internet is a human right'

我已更改托管磁盘块

FROM

resource "azurerm_managed_disk" "tf-mdsk-cluster" {
  count                = 5
  name                 = "${var.ax_base_hostname}-${count.index+1}-DATADISK-1"
  location             = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
  resource_group_name  = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "1024"
}

resource "azurerm_managed_disk" "tf-mdsk-2-cluster" {
  count                = 5
  name                 = "${var.ax_base_hostname}-${count.index+1}-DATADISK-2"
  location             = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
  resource_group_name  = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "1024"
}

resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
  count                 = 5
  name                  = "${var.ax_base_hostname}-${count.index+1}"
  location              = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
  resource_group_name   = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
  availability_set_id   = "${azurerm_availability_set.tf-as-cluster-aos.id}"
  network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index+1)}"]
  vm_size               = "${var.ax_vm_size}"

  storage_data_disk {
    name            = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.name, count.index+1)}"
    managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index+1)}"
    create_option   = "Attach"
    lun             = 0
    disk_size_gb    = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.disk_size_gb, count.index+1)}"
  }

  storage_data_disk {
    name            = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.name, count.index+1)}"
    managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.id, count.index+1)}"
    create_option   = "Attach"
    lun             = 1
    disk_size_gb    = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.disk_size_gb, count.index+1)}"
  }
}

TO

managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index+1)}"

但这还没有解决这个谜,关于如何解决它的任何想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您误会了某些地方,并将其设置为错误的设置。创建托管磁盘时,请使用count.index+1设置索引。然后,数据名称的编号为{1,2,3,4,5},索引为{0,1,2,3,4}。当您创建VM并在其中添加托管磁盘时。您也将storage_data_disk中的索引设置为count.index+1。因此,当索引为VM的1时,磁盘号将为2。

您可以将storage_data_disk中的索引更改为count.index。它将按您的意愿工作。 azurerm_virtual_machine会这样:

resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
  count                 = 5
  name                  = "${var.ax_base_hostname}-${count.index+1}"
  location              = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
  resource_group_name   = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
  availability_set_id   = "${azurerm_availability_set.tf-as-cluster-aos.id}"
  network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index+1)}"]
  vm_size               = "${var.ax_vm_size}"

  storage_data_disk {
    name            = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.name, count.index)}"
    managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index)}"
    create_option   = "Attach"
    lun             = 0
    disk_size_gb    = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.disk_size_gb, count.index)}"
  }

  storage_data_disk {
    name            = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.name, count.index)}"
    managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.id, count.index)}"
    create_option   = "Attach"
    lun             = 1
    disk_size_gb    = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.disk_size_gb, count.index)}"
  }
}