无法为特定 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")
}
}