我创建了虚拟机并添加了数据磁盘,但是有一个问题。虚拟机名称和数据磁盘名称不对齐。请参阅下面的屏幕截图。
terraform代码如下:
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)}"
}
}
我更改了托管磁盘块
从
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index+1)}"
至
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index)}"
但这还没有解开谜团,任何关于如何解决它的想法?
我认为你有一些误解并使其成为一个错误的环境。创建托管磁盘时,使用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)}"
}
}