我可以将磁盘广告到每个虚拟机,但是它们的名称未对齐。它造成混乱。
用于创建托管磁盘并将磁盘附加到托管磁盘的Terraform代码如下:
resource "azurerm_managed_disk" "tf-mdsk-cluster" {
count = 5
name = "${var.ax_base_hostname}-${count.index+1}-DATADISK"
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)}"
create_option = "Attach"
lun = 0
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.disk_size_gb, count.index+1)}"
}
让我们经历一下第一次迭代: 首先创建NXTPREPAOS-1-DATADISK ,并将其附加到 NXTPREPAOS-1 ,不确定为什么数据磁盘计数器和VM计数器不同步吗?
此外,如果我必须向每个VM添加另一个1 TB的数据磁盘,我的代码是否会像下面那样阻塞?
resource "azurerm_managed_disk" "tf-mdsk-1-cluster" {
count = 5
name = "${var.ax_base_hostname}-${count.index+1}-DATADISK"
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}-DATADISK2"
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)}"
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)}"
create_option = "Attach"
lun = 1
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.disk_size_gb, count.index+1)}"
}
答案 0 :(得分:2)
在引用其他资源时,您无需将count.index
递增1:
name = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.name, count.index+1)}"
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.disk_size_gb, count.index+1)}"
您的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)}"
}
您可能还会发现在此处也使用list[index]
syntax更为可取:
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 = "${azurerm_managed_disk.tf-mdsk-2-cluster.*.name[count.index]}"
managed_disk_id = "${azurerm_managed_disk.tf-mdsk-2-cluster.*.id[count.index]}"
create_option = "Attach"
lun = 1
disk_size_gb = "${azurerm_managed_disk.tf-mdsk-2-cluster.*.disk_size_gb[count.index]}"
}
答案 1 :(得分:0)
我发现引用给虚拟机的直接名称可以提供更一致的结果。允许事物保持同步。
resource "azurerm_managed_disk" "tf-mdsk-2-cluster" {
count = 5
name = "${azurerm_virtual_machine.tf-vm-cluster-aos[count.index].name}-DATADISK2"
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"
}