如何创建N个VM,每个VM创建并附加M个磁盘? [英] How do I create N VMs with M disks created and attached per VM?
问题描述
resource "azurerm_windows_virtual_machine" "virtual_machine" {
count = var.vm_count
name = "${local.vm_name}${count.index +1}"
resource "azurerm_virtual_machine_data_disk_attachment" "datadisk01" {
count = var.disk_count
**virtual_machine_id = azurerm_windows_virtual_machine.virtual_machine[count.index].id
managed_disk_id = element("${module.DISK.datadisk_id}","${count.index}")
}
问题-我有2个差异计数变量vm_count和disk_count.我想要一个通用的解决方案,例如.如果虚拟机计数为2,并且每个虚拟机应具有3个数据磁盘,即disk_count为3.
Issue - I have 2 diffenent count varaibles vm_count and disk_count. I want a generic solution, Ex. If vm count is 2 and each VM should have 3 datadisk i.e. disk_count is 3.
我能够根据计数创建多个VM和磁盘.但是在vm和磁盘附件时遇到问题,因为我无法在同一资源中使用2个计数.如何处理这种情况?
I am able to create multiple VMs and Disks as per the count. But facing issue while vm and disk attachment, as i am not able to use 2 counts in same resource. How to handle this situation?
面对的问题设为**
总而言之,我想创建vm_count(N)个虚拟机,并创建disk_count(M)个磁盘并将其附加到每个虚拟机.
In summary, I want to create vm_count (N) VMs, with disk_count (M) disks created and attached to each VM.
如何创建N个VM,每个VM创建并附加M个磁盘?
How do I create N VMs with M disks created and attached per VM?
推荐答案
您可以使用一组索引来实现以下目的:
You can achieve something like this using a set of indexes:
locals {
indexes = {
for vmi in range(var.vm_count): vmi => [
for ddi in range(var.disk_count):
ddi
]
}
}
这将创建这样的地图:
{
"0" = [
0,
1,
2,
]
"1" = [
0,
1,
2,
]
}
以后以这种方式使用:
resource "azurerm_virtual_machine_data_disk_attachment" "datadisk01" {
for_each = local.indexes
virtual_machine_id = azurerm_windows_virtual_machine.virtual_machine[tonumber(each.key)].id
managed_disk_id = element("${module.DISK.datadisk_id}","${each.value[0]}")
}
我还没有完全测试输出,因为VM需要更多配置.但是,正如我所看到的,您将 locals
用于 vm_name
,我将使用VM名称(包含索引,值和所需的磁盘)创建一个映射/对象.
I haven't fully tested the output as the VM needs more configuration. However as I can see you are using locals
for vm_name
I would create a map/object with VM names containing the index, the values and the desired disks.
我希望这有助于解决问题.
I hope this help resolve the issue.
这篇关于如何创建N个VM,每个VM创建并附加M个磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!