如何创建 'count.index' 以通过 Azure Provider 的 terraform 扩展 HDD
How to create a 'count.index' to scale the HDDs via terraform for Azure Provider
你想要完成什么?
按比例 向上 或 向下 'Resource VMs' 的数量与其必需的 'Resource OS_disk' 直接相关。例如,如果我们需要提供“5、10 或 20”VM,我们还需要匹配相同的“5、10 或 20”OS 磁盘.
目前,我知道如何将 count.index(请参阅下面的代码片段)用于 资源 VM,但我不知道如何为 资源 OS_Disk 实施它,这意味着如果我们需要提供“200 个虚拟机”,我可以通过设置该数量来定义那些“200 个虚拟机”通过来自 resource VM 代码块 的参数 count (参见下面的代码片段),但我不能为 '200' OS_disk(免责声明,再一次因为我不知道怎么做)
您有要分享的代码示例吗?
resource "azurerm_network_interface" "network_card_resource" {
count = 1
name = "network_card_${count.index}"
location = "removed"
resource_group_name = removed
ip_configuration {
name = "internal"
subnet_id = removed.outputs.removed.subnet.removed.main.subnets["removed-XX.XX.XX.XX_XX"].id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_windows_virtual_machine" "vm_resource" {
count = 10
name = "vm_name_${count.index}"
resource_group_name = removed
location = "removed"
size = "Standard_D4_v3"
admin_username = var.admin_username
admin_password = var.admin_password
network_interface_ids = [
element(azurerm_network_interface.network_card_resource.*.id, count.index)
]
os_disk {
count = 10
name = "disk_name_${count.index}"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
resource_group_name = removed
disk_size_gb = 180
}
以上代码无法通过 count.index,[=12] 构建 10 OS 个磁盘 =]
有解决这个问题的方法吗?
无法为特定 VM 添加 10 OS 磁盘,你 每个 VM 只能有 1 OS 个磁盘,除此之外 您可以附加数据磁盘 .
因此,作为一种解决方案,您可以使用 for_each
而不是 count
以获得更好的代码功能,然后您可以使用以下脚本根据您的需要创建具有额外数据磁盘的 VM要求:
在下面的代码中,我创建了 3 个虚拟机,每个虚拟机有 1 个 OS 磁盘和 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")
}
}
你想要完成什么?
按比例 向上 或 向下 'Resource VMs' 的数量与其必需的 'Resource OS_disk' 直接相关。例如,如果我们需要提供“5、10 或 20”VM,我们还需要匹配相同的“5、10 或 20”OS 磁盘.
目前,我知道如何将 count.index(请参阅下面的代码片段)用于 资源 VM,但我不知道如何为 资源 OS_Disk 实施它,这意味着如果我们需要提供“200 个虚拟机”,我可以通过设置该数量来定义那些“200 个虚拟机”通过来自 resource VM 代码块 的参数 count (参见下面的代码片段),但我不能为 '200' OS_disk(免责声明,再一次因为我不知道怎么做)
您有要分享的代码示例吗?
resource "azurerm_network_interface" "network_card_resource" {
count = 1
name = "network_card_${count.index}"
location = "removed"
resource_group_name = removed
ip_configuration {
name = "internal"
subnet_id = removed.outputs.removed.subnet.removed.main.subnets["removed-XX.XX.XX.XX_XX"].id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_windows_virtual_machine" "vm_resource" {
count = 10
name = "vm_name_${count.index}"
resource_group_name = removed
location = "removed"
size = "Standard_D4_v3"
admin_username = var.admin_username
admin_password = var.admin_password
network_interface_ids = [
element(azurerm_network_interface.network_card_resource.*.id, count.index)
]
os_disk {
count = 10
name = "disk_name_${count.index}"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
resource_group_name = removed
disk_size_gb = 180
}
以上代码无法通过 count.index,[=12] 构建 10 OS 个磁盘 =]
有解决这个问题的方法吗?
无法为特定 VM 添加 10 OS 磁盘,你 每个 VM 只能有 1 OS 个磁盘,除此之外 您可以附加数据磁盘
因此,作为一种解决方案,您可以使用 for_each
而不是 count
以获得更好的代码功能,然后您可以使用以下脚本根据您的需要创建具有额外数据磁盘的 VM要求:
在下面的代码中,我创建了 3 个虚拟机,每个虚拟机有 1 个 OS 磁盘和 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")
}
}