0

你想完成什么?

增加减少与其必需的“资源 OS_disk”直接相关的“资源 VM”的数量。例如,如果我们需要配置“5、10 或 20”个VM,我们还需要匹配相同的“5、10 或 20”个操作系统磁盘

目前,我知道如何为资源 VM使用count.index(参见下面的代码片段),但我不知道如何为资源 OS_Disk实现它,这意味着如果我们需要提供“200 个 VM”,我可以定义通过从资源 VM 代码块中的参数计数设置这些“200 个 VM” (参见下面的代码片段),但我不能为“200” OS_disk(免责声明,再次因为我不知道如何)

你有代码示例要分享吗?

resource "azurerm_network_interface" "network_card_resource" {
  count                     = 1
  name                      = "network_card_${count.index}"
  location                  = "removed"
  resource_group_name       = removed

  ip_configuration {
    name                          = "internal"
    subnet_id                     = removed.outputs.removed.subnet.removed.main.subnets["removed-XX.XX.XX.XX_XX"].id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "vm_resource" {
  count                     = 10
  name                      = "vm_name_${count.index}"
  resource_group_name       = removed
  location                  = "removed"
  size                      = "Standard_D4_v3"


  admin_username            = var.admin_username
  admin_password            = var.admin_password
  network_interface_ids     = [
    element(azurerm_network_interface.network_card_resource.*.id, count.index)
  ]

  os_disk {
    count                   = 10
    name                    = "disk_name_${count.index}"
    caching                 = "ReadWrite"
    storage_account_type    = "Standard_LRS"
    resource_group_name     = removed
    disk_size_gb            = 180
  }

上述代码未能通过count.index构建 10 个OS 磁盘

有没有办法解决这个问题?

4

1 回答 1

0

无法为特定 VM 添加 10 个 OS 磁盘,每个VM 只能有 1 个 OS 磁盘,除此之外,您可以附加数据磁盘 (2 disks for 1 VM VCPU)

因此,作为一种解决方案,您可以使用for_each而不是count更好的代码功能,然后您可以使用以下脚本根据您的要求使用额外的数据磁盘创建 VM:

在下面的代码中,我创建了 3 个虚拟机,每个虚拟机有 1 个操作系统磁盘和 5 个数据磁盘:

provider "azurerm"{
    features{}
}

data "azurerm_resource_group" "example"{
    name="ansumantest"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
}


resource "azurerm_subnet" "test_subnet" {
    name = "VM-subnet"
    resource_group_name = data.azurerm_resource_group.example.name
    virtual_network_name = azurerm_virtual_network.example.name
    address_prefixes = ["10.0.0.0/24"]
}

resource "azurerm_public_ip" "myterraformpublicip" {
   for_each = toset(var.instances)
   name                         = "myPublicIP-${each.key}"
   location                     = data.azurerm_resource_group.example.location
   resource_group_name          = data.azurerm_resource_group.example.name
   allocation_method            = "Dynamic"
}

resource "azurerm_network_interface" "myterraformnic" {
    for_each= toset(var.instances)
    name                      = "myNIC-${each.key}"
    location                  = data.azurerm_resource_group.example.location
    resource_group_name       = data.azurerm_resource_group.example.name

    ip_configuration {
      name                          = "myNicConfiguration"
      subnet_id                     = azurerm_subnet.test_subnet.id
      private_ip_address_allocation = "Dynamic"
      public_ip_address_id          = azurerm_public_ip.myterraformpublicip[each.key].id
    }
}
  variable "instances" {
  default = ["vm-test-1", "vm-test-2", "vm-test-3"]
  }
  variable "nb_disks_per_instance" {
  default = 5
  }
locals {
  vm_datadiskdisk_count_map = { for k in toset(var.instances) : k => var.nb_disks_per_instance }
  luns                      = { for k in local.datadisk_lun_map : k.datadisk_name => k.lun }
  datadisk_lun_map = flatten([
    for vm_name, count in local.vm_datadiskdisk_count_map : [
      for i in range(count) : {
        datadisk_name = format("datadisk_%s_disk%02d", vm_name, i)
        lun           = i
      }
    ]
  ])
}

resource "azurerm_windows_virtual_machine" "example" {
  for_each = toset(var.instances)
  name                = "example-machine${each.key}"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = data.azurerm_resource_group.example.location
  size                = "Standard_F2"
  admin_username      = "adminuser"
  admin_password      = "P@$$w0rd1234!"
  network_interface_ids = [azurerm_network_interface.myterraformnic[format("%s", each.key)].id]

   os_disk {
    name                    = "${each.key}-OSDISK"
    caching                 = "ReadWrite"
    storage_account_type    = "Standard_LRS"
    disk_size_gb            = 180
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

resource "azurerm_managed_disk" "example" {
  for_each             = toset([for j in local.datadisk_lun_map : j.datadisk_name])
  name                 = each.key
  location             = data.azurerm_resource_group.example.location
  resource_group_name  = data.azurerm_resource_group.example.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = 10
}

resource "azurerm_virtual_machine_data_disk_attachment" "example" {
  for_each           = toset([for j in local.datadisk_lun_map : j.datadisk_name])
  managed_disk_id    = azurerm_managed_disk.example[each.key].id
  virtual_machine_id = azurerm_windows_virtual_machine.example[element(split("_", each.key), 1)].id
  lun                = lookup(local.luns, each.key)
  caching            = "ReadWrite"
}

输出:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述


通过在 OS_disk 块内提供另一个计数,不需要将 OS 磁盘与计数匹配:

例如,我使用 terraform 创建了 2 个虚拟机,然后默认情况下也会创建 2 个 OS 磁盘,如下所示:

resource "azurerm_linux_virtual_machine" "myterraformvm" {
count = 2
name                  = "testpoc0${count.index + 1}"
location              = data.azurerm_resource_group.example.location
resource_group_name   = data.azurerm_resource_group.example.name
 #network_interface_ids = azurerm_network_interface.myterraformnic.*.id
network_interface_ids = [element(azurerm_network_interface.myterraformnic.*.id, count.index + 1)]
size                  = "Standard_DS1_v2"

os_disk {
    ## count == not required here again as this is a child block inside the main VM resource block , so as many VM's will be created OS disk will also be created that many
    name              = "OsDisk${count.index + 1}"
    caching           = "ReadWrite"
    storage_account_type = "Premium_LRS"
}

source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
}

computer_name  = "testpoc0${count.index}"
admin_username = "azureuser"
disable_password_authentication = true

admin_ssh_key {
    username       = "azureuser"
    public_key     = file("~/.ssh/id_rsa.pub")
}
}

在此处输入图像描述

在此处输入图像描述

于 2022-01-11T08:02:25.490 回答