使用 for_each 将不同数量的磁盘连接到不同数量的 VM

Use a for_each to attach varying number of disks to varying number of VM's

TF 版本:0.13.4

提供商版本:2.40.0

我不确定 best/most 执行此操作的有效方法,但我想要实现的是为每个 VM 附加 2 个磁盘(目前),但我需要能够根据要求配置每个 VM 和磁盘。将来我可能需要改变磁盘和虚拟机的数量。我试图让 for_each 遍历大小并根据其中有多少个值,创建该数量的磁盘,以便我可以拥有多个不同大小的磁盘。

有没有无需手动创建多个托管磁盘资源即可执行此操作的方法?

我当前的代码创建了不同数量的 VM,但每个 VM 仅附加一个磁盘。 for_each 迭代的变量在每个环境的 tfvars 文件中设置:

desktop_servers = {
  "Server_1" = {
    name = 1,
    zone = 1,
    lun  = 1,
    size = 32
  }
  "Server_2" = {
    name = 2,
    zone = 2,
    lun  = 2,
    size = 32
  }

  "Server_3" = {
    name = 3,
    zone = 3,
    lun  = 3,
    size = 32
  }
}

etl_servers = {
  "Server_1" = {
    name = 1,
    zone = 1,
    lun  = 1,
    size = 32
  }
  "Server_2" = {
    name = 2,
    zone = 2,
    lun  = 2,
    size = 32
  }
}

module.tf

内的虚拟机资源
# Azure Virtual Machine
resource "azurerm_windows_virtual_machine" "virtual_machine" {
  for_each                         = var.servers
  name                             = "vm-${var.environment}-${var.vm_identifier}${each.value.name}"
  location                         = var.location
  resource_group_name              = var.resource_group
  zone                             = each.value.zone
  size                             = var.vm_size
  network_interface_ids            = [azurerm_network_interface.network_interface[each.key].id]
  computer_name                    = "${var.vm_identifier}${each.value.name}"
  admin_username                   = xxxx
  admin_password                   = xxxx
  provision_vm_agent               = "true"
  source_image_id                  = data.azurerm_shared_image.dwp_shared_image.id


  boot_diagnostics {
    storage_account_uri = data.azurerm_storage_account.dwp_diag_storage_account.primary_blob_endpoint
  }

  os_disk {
    name                      = "vm-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}-os${each.value.name}"
    caching                   = "ReadWrite"
    storage_account_type      = "Premium_LRS"
  }

  depends_on = [azurerm_network_interface.network_interface]
}

module.tf 内的托管磁盘资源:

# #Managed disks per vm
resource "azurerm_managed_disk" "managed_disk" {
  for_each               = var.servers
  name                   = "disk-${var.environment}-${var.vm_identifier}${each.value.name}"
  location               = var.location
  resource_group_name    = var.resource_group
  storage_account_type   = "Premium_LRS"
  create_option          = "Empty"
  disk_size_gb           = each.value.size
  zones                  = [azurerm_windows_virtual_machine.virtual_machine[each.key].zone]
}

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attachment" {
  for_each           = var.servers
  managed_disk_id    = azurerm_managed_disk.managed_disk[each.key].id
  virtual_machine_id = azurerm_windows_virtual_machine.virtual_machine[each.key].id
  lun                = each.value.lun
  caching            = "ReadWrite"
}

variable.tf 模块内:

variable "servers" {
  description = "Variable for defining each instance"
}

main.tf 中 ETL 服务器的模块示例。 :

module "etl_vm" {
  source                       = "../modules/compute/windows_vm"
  location                     = var.location
  resource_group               = azurerm_resource_group.rg_sbox_etl.name
  directorate                  = var.directorate
  business_unit                = var.business_unit
  environment                  = var.environment
  network_rg_identifier        = var.network_rg_identifier
  subnet_name                  = "sub-${var.environment}-${var.directorate}-${var.business_unit}-be01"
  diag_storage_account_name    = var.diag_storage_account_name
  log_analytics_workspace_name = var.log_analytics_workspace_name
  backup_policy_name           = var.backup_policy_name
  vm_identifier                = "${var.vm_identifier}${var.instance_number}-etl"
  servers                      = var.etl_servers
  vm_size                      = var.etl_vm_size
  enable_management_locks      = true
  image_name                   = "WIN2016-CISL2"
  gallery_subscription_id      = var.sub_id
  gallery_resourcegroup        = var.rg_gallery
  gallery_name                 = "SBOXGallery"

您可以使用模块来实现。但这有点复杂。这个想法是你使用模块来配置VM,在每个VM中,你可以配置不同的数据磁盘并将它们附加到VM。使用变量输入配置的不同 VM 编号。这是一个例子:

main.tf

variable "rg_name" {
    type = string
}

variable "vms" {
    type = map(object({
        size = string
        admin_user = string
        admin_password = string
        disks = list(number)
    }))
}

variable "location" {}

module "vms" {
    for_each = var.vms
    
    source = "./modules/vm"
    resource_group_name = var.rg_name
    vm_name = each.key
    vm = each.value
    disks = each.value["disks"]
    location = var.location
}

terraform.tfvar

rg_name = "charlesVMs"
location = "East US"
vms ={
  azurevm1 = {
    
    size = "Standard_DS1_v2"
    admin_user = "azureuser"
    admin_password = "azureuser@2021"
    disks = [30, 30]
  }
}

./modules/vm/main.tf

variable "resource_group_name" {}

variable "vm" {}

variable "disks" {}

variable "location" {}

variable "vm_name" {}

resource "azurerm_resource_group" "example" {
    name  = var.resource_group_name
    location = var.location
}

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

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "vm" {
  name              = var.vm_name
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = var.vm["size"]

  disable_password_authentication = false
  admin_username      = var.vm["admin_user"]
  admin_password      = var.vm["admin_password"]
  network_interface_ids = [
    azurerm_network_interface.example.id
  ]

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

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

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

resource "azurerm_managed_disk" "example" {
  count       = length(var.vm["disks"])
  name        = "datadisk-${count.index}"
  location    = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb    = element(var.vm["disks"], count.index)
}

resource "azurerm_virtual_machine_data_disk_attachment" "example" {
  count              = length(var.vm["disks"])
  managed_disk_id    = element(azurerm_managed_disk.example.*.id, count.index)
  virtual_machine_id = azurerm_linux_virtual_machine.vm.id
  lun                = count.index
  caching            = "ReadWrite"
}

您只需要为 vms 变量提供不同数量的 VM 配置和不同数量的磁盘以及大小。