使用Terraform将磁盘附加到多个虚拟机

huangapple go评论67阅读模式
英文:

Attach disks to multiple VMs using Terraform

问题

I want to attach disks of different sizes to a particular VM. My code is as follows. 
"Virtual_machine_id" was imported through for loop, but could not be written.
My "output" works fine, but it actually causes problems with the "terraform plan".

- manageddisk.tf
```plaintext
我想将不同大小的磁盘附加到特定的虚拟机上。我的代码如下。
"Virtual_machine_id" 是通过循环导入的,但无法编写。
我的"output"工作正常,但实际上会导致"terraform plan"出现问题。

- manageddisk.tf
  • var.tf
variable "vm_template" {
  type = map(any)
  default = {
    vm1 = {
      vm_name           = "test01"
      os_publisher      = "MicrosoftWindowsServer"
      os_offer          = "WindowsServer"
      os_sku            = "2022-datacenter-azure-edition"
      license_type      = "Windows_Server"
      os_version        = "latest"
      osdisk_type       = "StandardSSD_LRS"
      osdisk_size       = "1024"
      vm_type           = "Standard_D4s_v5"
    }
    vm2 = {
      vm_name           = "test02"
      os_publisher      = "MicrosoftWindowsServer"
      os_offer          = "WindowsServer"
      os_sku            = "2022-datacenter-azure-edition"
      license_type      = "Windows_Server"
      os_version        = "latest"
      osdisk_type       = "StandardSSD_LRS"
      osdisk_size       = "1024"
      vm_type           = "Standard_D4s_v5"
    }
  }
}

variable "disk_template" {
  type = map(any)
  default = {
    disk1 = {
      managed_disk_name = "data"
      managed_disk_type = "Standard_LRS"
      managed_disk_size = "10"
    }
    disk2 = {
      managed_disk_name = "log"
      managed_disk_type = "Standard_LRS"
      managed_disk_size = "10"
    }
    disk3 = {
      managed_disk_name = "backup"
      managed_disk_type = "Standard_LRS"
      managed_disk_size = "10"
    }
  }
}

However, virtual_machine_id is the problem.

然而,virtual_machine_id 是问题所在。

Successfully attaches disk by acquiring "virtual_machine_id".

通过获取"virtual_machine_id"成功附加磁盘。

I succeeded with the code below. But not yet.

我使用下面的代码成功了。但还没有。

I didn't understand this.

我不明白这个。

Managed Disk

# 托管磁盘

Please let me know if you need further assistance.

英文:

I want to attach disks of different sizes to a particular VM. My code is as follows.
"Virtual_machine_id" was imported through for loop, but could not be written.
My "output" works fine, but it actually causes problems with the "terraform plan".

  • manageddisk.tf
## 
data "azurerm_virtual_machine" "vm_id" {
  for_each = var.vm_template
  name = each.value.vm_name
  resource_group_name  = data.azurerm_resource_group.system_rg_name.name
}

resource "azurerm_managed_disk" "managed_disk" {
  for_each = {
    for key, value in var.disk_template : key => value
    if value.managed_disk_size != "0"
  }
  name                 = "${each.value.managed_disk_name}"
  location             = var.location
  resource_group_name  = data.azurerm_resource_group.system_rg_name.name
  storage_account_type = each.value.managed_disk_type
  create_option        = "Empty"
  disk_size_gb         = each.value.managed_disk_size
}

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" {
  for_each = {
    for key, value in var.disk_template : key => value
    if value.managed_disk_size != "0"
  }
  managed_disk_id    = azurerm_managed_disk.managed_disk[each.key].id
  lun                = each.value.managed_disk_size
  caching            = "ReadWrite"
  virtual_machine_id   = { for k,v in data.azurerm_virtual_machine.vm_id : k => v.id }
}

output "vm_id" {
  value = { for k,v in data.azurerm_virtual_machine.vm_id : k => v.id }
}
  • var.tf
variable "vm_template" {
  type = map(any)
  default = {
    vm1 = {
      vm_name           = "test01"
      os_publisher      = "MicrosoftWindowsServer"
      os_offer          = "WindowsServer"
      os_sku            = "2022-datacenter-azure-edition"
      license_type      = "Windows_Server"
      os_version        = "latest"
      osdisk_type       = "StandardSSD_LRS"
      osdisk_size       = "1024"
      vm_type           = "Standard_D4s_v5"
    }
    vm2 = {
      vm_name           = "test02"
      os_publisher      = "MicrosoftWindowsServer"
      os_offer          = "WindowsServer"
      os_sku            = "2022-datacenter-azure-edition"
      license_type      = "Windows_Server"
      os_version        = "latest"
      osdisk_type       = "StandardSSD_LRS"
      osdisk_size       = "1024"
      vm_type           = "Standard_D4s_v5"
    }
  }
}

variable "disk_template" {
  type = map(any)
  default = {
    disk1 = {
      managed_disk_name = "data"
      managed_disk_type = "Standard_LRS" # Standard_LRS StandardSSD_ZRS Premium_LRS PremiumV2_LRS Premium_ZRS StandardSSD_LRS UltraSSD_LRS
      managed_disk_size = "10"
    }
    disk2 = {
      managed_disk_name = "log"
      managed_disk_type = "Standard_LRS" # Standard_LRS StandardSSD_ZRS Premium_LRS PremiumV2_LRS Premium_ZRS StandardSSD_LRS UltraSSD_LRS
      managed_disk_size = "10"
    }
    disk3 = {
      managed_disk_name = "backup"
      managed_disk_type = "Standard_LRS" # Standard_LRS StandardSSD_ZRS Premium_LRS PremiumV2_LRS Premium_ZRS StandardSSD_LRS UltraSSD_LRS
      managed_disk_size = "10"
    }
  }
}

However, virtual_machine_id is the problem.

╷
│ Error: Incorrect attribute value type
│
│   on manageddisk.tf line 36, in resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach":
│   36:   virtual_machine_id   = { for k,v in data.azurerm_virtual_machine.vm_id : k => v.id }
│     ├────────────────
│     │ data.azurerm_virtual_machine.vm_id is object with 2 attributes
│
│ Inappropriate value for attribute "virtual_machine_id": string required.
╵
╷
│ Error: Incorrect attribute value type
│
│   on manageddisk.tf line 36, in resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach":
│   36:   virtual_machine_id   = { for k,v in data.azurerm_virtual_machine.vm_id : k => v.id }
│     ├────────────────
│     │ data.azurerm_virtual_machine.vm_id is object with 2 attributes
│
│ Inappropriate value for attribute "virtual_machine_id": string required.
╵
╷
│ Error: Incorrect attribute value type
│
│   on manageddisk.tf line 36, in resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach":
│   36:   virtual_machine_id   = { for k,v in data.azurerm_virtual_machine.vm_id : k => v.id }
│     ├────────────────
│     │ data.azurerm_virtual_machine.vm_id is object with 2 attributes
│
│ Inappropriate value for attribute "virtual_machine_id": string required.
╵

Successfully attaches disk by acquiring "virtual_machine_id".

I succeeded with the code below. But not yet.

vm = vm

I didn't understand this.

# ############################
# ### Managed Disk
# ############################
locals {
  disk_vm_flat = merge([
    for vm, vm_vals in var.vm_template : {
      for disk, disk_vals in var.disk_template :
      "${vm_vals.vm_name}-${disk_vals.managed_disk_name}" => {
        vm      = vm
        vm_name = vm_vals.vm_name
        managed_disk_name = disk_vals.managed_disk_name
        managed_disk_size = disk_vals.managed_disk_size
        managed_disk_lun  = disk_vals.managed_disk_lun
        managed_disk_type = disk_vals.managed_disk_type
      } if disk_vals.managed_disk_size != "0"
    }
  ]...)
}

resource "azurerm_managed_disk" "managed_disk" {
  for_each             = local.disk_vm_flat
  name                 = "${each.value.vm_name}-${each.value.managed_disk_name}"
  location             = var.location
  resource_group_name = data.azurerm_resource_group.system_rg_name.name
  storage_account_type = each.value.managed_disk_type
  create_option        = "Empty"
  disk_size_gb         = each.value.managed_disk_size
}

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" {
  for_each           = local.disk_vm_flat
  managed_disk_id    = azurerm_managed_disk.managed_disk[each.key].id
  lun                = each.value.managed_disk_lun
  caching            = "ReadWrite"
  virtual_machine_id = azurerm_windows_virtual_machine.vm[each.value.vm].id
}

答案1

得分: 0

你基本上需要一个双重循环,在使用之前需要将其展开:

locals {
  disk_vm_flat = merge([
    for vm, vm_vals in var.vm_template : {
      for disk, disk_vals in var.disk_template :
        "${vm_vals.vm_name}-${disk_vals.managed_disk_name}" => {
            vm_name = vm
            managed_disk_name = disk
            managed_disk_size = disk_vals.managed_disk_size
        } if disk_vals.managed_disk_size != "0"
    }
  ]...) # 不要删除这些点,它们很重要
}

然后

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" {
  for_each           = local.disk_vm_flat
  managed_disk_id    = azurerm_managed_disk.managed_disk[each.value.managed_disk_name].id
  lun                = each.value.managed_disk_size
  caching            = "ReadWrite"
  virtual_machine_id   = data.azurerm_virtual_machine.vm_id[each.value.vm_name].id
}
英文:

You basically need a double for loop, which you need to flatten first before the use:

locals {
  disk_vm_flat = merge([
    for vm, vm_vals in var.vm_template:  {
      for disk, disk_vals in var.disk_template:
        "${vm_vals.vm_name}-${disk_vals.managed_disk_name}" => {
            vm_name = vm
            managed_disk_name = disk
            managed_disk_size = disk_vals.managed_disk_size
        }  if disk_vals.managed_disk_size != "0"
    }
  ]...) # do NOT remove the dots. they are important
} 

then

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" {
  for_each           = local.disk_vm_flat
  managed_disk_id    = azurerm_managed_disk.managed_disk[each.value.managed_disk_name].id
  lun                = each.value.managed_disk_size
  caching            = "ReadWrite"
  virtual_machine_id   = data.azurerm_virtual_machine.vm_id[each.value.vm_name].id
}

huangapple
  • 本文由 发表于 2023年3月3日 22:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定