azure - 使用 for_each 将不同数量的磁盘附加到不同数量的虚拟机

标签 azure azure-virtual-machine terraform-provider-azure

TF版本:0.13.4

提供程序版本:2.40.0

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

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

我当前的代码创建了不同数量的虚拟机,但每个虚拟机仅附加一个磁盘。 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。使用该变量输入配置的不同虚拟机编号。这是一个例子:

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 变量提供不同数量的虚拟机配置和不同数量的磁盘大小。

关于azure - 使用 for_each 将不同数量的磁盘附加到不同数量的虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65579265/

相关文章:

Azure - 有没有办法在捕获图像后概括图像?

c# - 在 Windows Azure 虚拟机上设置时区

Azure 托管身份和 Terraform 无法在 AzureUSGoverment 中运行

azure - 是否可以匿名调用API App并自动将浏览器重定向到AD登录页面?

sql-server - 弹性池的 VCore 扩展(例如从 2 到 4)与使用 VCore 创建新池 (4) 和移动数据库的延迟

azure - 无法打开 Azure VM 打开端口

azure - Terraform 数据源列表和筛选现有 Azure 资源组

azure - 将本地 tf 状态文件指定给管道中的 azurerm 提供程序

azure - 限制从 Azure VM 对 Azure SQL 的访问

Azure Devops 部署批准