如何为 Azure 可用性集中的虚拟机数量创建 Terraform for_each 循环
how to create a Terraform for_each loop for number of virtual machines in Azure availability set
我们可以使用 "count" 循环在可用性集中创建多个 azure 虚拟机。
我们如何使用 "for_each" 循环创建相同的主机名和网络接口 ID 将是动态的并循环。 (在 terrraform > 0.12.6 中)
resource "azurerm_virtual_machine" "test" {
# user provides inputs only for the number of vms to be created in the Azure avaialibility set
count = var.count
name = "acctvm${count.index}"
location = azurerm_resource_group.test.location
availability_set_id = azurerm_availability_set.avset.id
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [element(azurerm_network_interface.test.*.id, count.index)]
vm_size = "Standard_DS1_v2"
tags = var.tags
For-each 需要一组循环。我假设您使用变量作为输入,所以
variable "vms" {
type = list(string)
default = ["alpha", "beta"]
}
variable "vms_data" {
type = map(map(string))
default = {
alpha = {
hostname = "alpha"
interfaceid = "01"
}
alpha = {
hostname = "beta"
interfaceid = "02"
}
}
}
resource "azurerm_virtual_machine" "test" {
for_each = toset(var.vms)
name = var.vms_data[each.value].hostname
location = azurerm_resource_group.test.location
availability_set_id = azurerm_availability_set.avset.id
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [
element(azurerm_network_interface.test.*.id, var.vms_data[each.value].interfaceid)]
vm_size = "Standard_DS1_v2"
tags = var.tags
}
但尚未在 Azure 中实现 (v. 12.23)。我收到一个错误 The name "for_each" is reserved for use in a future version of Terraform.
您可以在对象列表中指定所需的 VM 属性,然后使用 for_each
循环,如下所示:
variable "VirtualMachines" {
type = list(object({
hostname= string
interfaceid = string
}))
default = [
{
hostname= "VM01",
interfaceid = "01"
},
{
hostname= "VM02",
interfaceid = "02"
}
]
}
resource "azurerm_virtual_machine" "test" {
for_each = {for vm in var.VirtualMachines: vm.hostname => vm}
name = each.value.hostname
location = azurerm_resource_group.test.location
availability_set_id = azurerm_availability_set.avset.id
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [each.value.interfaceid]
vm_size = "Standard_DS1_v2"
tags = var.tags
}
我们可以使用 "count" 循环在可用性集中创建多个 azure 虚拟机。
我们如何使用 "for_each" 循环创建相同的主机名和网络接口 ID 将是动态的并循环。 (在 terrraform > 0.12.6 中)
resource "azurerm_virtual_machine" "test" {
# user provides inputs only for the number of vms to be created in the Azure avaialibility set
count = var.count
name = "acctvm${count.index}"
location = azurerm_resource_group.test.location
availability_set_id = azurerm_availability_set.avset.id
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [element(azurerm_network_interface.test.*.id, count.index)]
vm_size = "Standard_DS1_v2"
tags = var.tags
For-each 需要一组循环。我假设您使用变量作为输入,所以
variable "vms" {
type = list(string)
default = ["alpha", "beta"]
}
variable "vms_data" {
type = map(map(string))
default = {
alpha = {
hostname = "alpha"
interfaceid = "01"
}
alpha = {
hostname = "beta"
interfaceid = "02"
}
}
}
resource "azurerm_virtual_machine" "test" {
for_each = toset(var.vms)
name = var.vms_data[each.value].hostname
location = azurerm_resource_group.test.location
availability_set_id = azurerm_availability_set.avset.id
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [
element(azurerm_network_interface.test.*.id, var.vms_data[each.value].interfaceid)]
vm_size = "Standard_DS1_v2"
tags = var.tags
}
但尚未在 Azure 中实现 (v. 12.23)。我收到一个错误 The name "for_each" is reserved for use in a future version of Terraform.
您可以在对象列表中指定所需的 VM 属性,然后使用 for_each
循环,如下所示:
variable "VirtualMachines" {
type = list(object({
hostname= string
interfaceid = string
}))
default = [
{
hostname= "VM01",
interfaceid = "01"
},
{
hostname= "VM02",
interfaceid = "02"
}
]
}
resource "azurerm_virtual_machine" "test" {
for_each = {for vm in var.VirtualMachines: vm.hostname => vm}
name = each.value.hostname
location = azurerm_resource_group.test.location
availability_set_id = azurerm_availability_set.avset.id
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [each.value.interfaceid]
vm_size = "Standard_DS1_v2"
tags = var.tags
}