Terraform в Azure, имя виртуальной машины и диск с управляемыми данными не выровнены - PullRequest
0 голосов
/ 19 февраля 2019

Я могу добавить диск к каждой виртуальной машине, но их имена не выровнены.Это создает путаницу.

enter image description here

enter image description here

Код 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 , не знаю почемуСчетчик диска данных и счетчик виртуальной машины не синхронизированы?

Кроме того, если бы мне пришлось добавить еще один диск данных объемом 1 ТБ для каждой виртуальной машины, будет ли мой код блокироваться, как показано ниже?

 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)}"
     }

1 Ответ

0 голосов
/ 19 февраля 2019

Вам не нужно увеличивать 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] :

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]}"
 }
...