从另一个 terraform 块中引用每个循环的每个键的值

问题描述 投票:0回答:1

我尝试使用每个循环以及每个.key 和每个值配置在 Azure 中部署 2 个虚拟机。但是,我在下面的network_interface_ids参数中收到一条错误消息,其中NIC的each.key定义的值指向VM的名称而不是NIC名称ID。我在配置中遗漏了什么吗?感谢您的宝贵意见。

我的示例代码:

locals {
  virtual_machines = {
    (var.vm_win_name) = (var.location)
    (var.vm_win_name-test) = (var.location) 
  }
}

locals {
  vm_nic_interfaces = {
    (var.vm_win_nic_name) = (var.location)
    (var.vm_win_nic_name-test) = (var.location) 
  }
}

variable "vm_win_name" {
  description = "The name of VM"
  type = string
  default = "Testvm01"
}

variable "vm_win_name-test" {      
  description = "The name of VM"
  type = string
  default = "Testvm02"
}

variable "vm_win_nic_name" {
  type = string
  default = "Testvm01-nic"
}

variable "vm_win_nic_name-test" { 
  type = string
  default = "Testvm02-nic"
}

resource "azurerm_network_interface" "nic_win_jumpbox" {
  for_each = local.vm_nic_interfaces

  name = each.key
  location = each.value
  resource_group_name = azurerm_resource_group.resource_groups[var.mgmt_resource_group_name].name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = data.azurerm_subnet.subnet_ids[2].id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "win_jumpbox" {
  for_each = local.virtual_machines

  name = each.key
  location = each.value
  resource_group_name = azurerm_resource_group.resource_groups[var.mgmt_resource_group_name].name
  network_interface_ids = [azurerm_network_interface.nic_win_jumpbox[each.key].id]

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2022-datacenter-azure-edition-hotpatch"
    version = "latest"
  }

  size = var.vm_size
  admin_username = "azureadmin"
  admin_password = "Testvm1234&
  enable_automatic_updates = false
  patch_mode = "AutomaticByPlatform"

  tags = local.resource_groups_tags


  os_disk {
    name = each.key
    caching = "ReadWrite"
    storage_account_type = "StandardSSD_LRS"

}

错误信息:

│     │ azurerm_network_interface.nic_win_jumpbox is object with 2 attributes
│     │ each.key is "Testvm01"
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on winvm.tf line 44, in resource "azurerm_windows_virtual_machine" "win_jumpbox":
│   44:   network_interface_ids = [azurerm_network_interface.nic_win_jumpbox[each.key].id]
│     ├────────────────
│     │ azurerm_network_interface.nic_win_jumpbox is object with 2 attributes
│     │ each.key is "Testvm02"
│ 
│ The given key does not identify an element in this collection value.

我尝试修改 network_interface_ids 参数中的可能值,但错误仍然存在。

azure terraform foreach-loop-container
1个回答
0
投票

您的代码设计可以改进,希望您不介意一些建议:

  1. 使用映射而不是为每个虚拟机或网卡声明多个变量。
  2. VM 和 NIC 之间存在 1:1 关系,因此可以将它们分组在一起

使用示例:

providers.tf
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.101.0"
    }
  }
}

provider "azurerm" {
  features {}
}
变量.tf

var.virtual_machines
设置为
map
对象 - 这允许我们添加、更改或删除 VM/NIC,而无需触及其余代码。

variable "mgmt_resource_group_name" {
  description = "The name of the resource group"
  type        = string
  default     = "myResourceGroup"
}

variable "mgmt_resource_group_location" {
  description = "The location of the resource group"
  type        = string
  default     = "East US"
}

variable "virtual_machines" {
  description = "Virtual machines configuration."
  type = map(object({
    name     = string,
    vm_size  = optional(string, "Standard_DS1_v2"),
    nic_name = string
  }))
  default = {
    "01" = {
      name     = "Testvm01",
      nic_name = "Testvm01-nic"
    },
    "02" = {
      name     = "Testvm02",
      nic_name = "Testvm02-nic"
    }
  }
}

variable "resource_groups_tags" {
  description = "Tags"
  type        = map(string)
  default = {
    foo = "bar"
  }
}
主.tf
resource "azurerm_resource_group" "resource_group" {
  name     = var.mgmt_resource_group_name
  location = var.mgmt_resource_group_location

  tags = var.resource_groups_tags
}

resource "azurerm_network_interface" "nic_win_jumpbox" {
  for_each = var.virtual_machines

  name                = each.value.nic_name
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name

  ip_configuration {
    name = "internal"
  # subnet_id                     = data.azurerm_subnet.subnet_ids[2].id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "win_jumpbox" {
  for_each = var.virtual_machines

  name                = each.value.name
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name

  network_interface_ids = [
    azurerm_network_interface.nic_win_jumpbox[each.key].id
  ]

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2022-datacenter-azure-edition-hotpatch"
    version   = "latest"
  }

  size                     = each.value.vm_size
  admin_username           = "azureadmin"
  admin_password           = "Testvm1234&"
  enable_automatic_updates = false
  patch_mode               = "AutomaticByPlatform"

  tags = var.resource_groups_tags

  os_disk {
    name                 = each.value.name
    caching              = "ReadWrite"
    storage_account_type = "StandardSSD_LRS"
  }
}

跑步
terraform plan
:

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_network_interface.nic_win_jumpbox["01"] will be created
  + resource "azurerm_network_interface" "nic_win_jumpbox" {
      + applied_dns_servers           = (known after apply)
      + dns_servers                   = (known after apply)
      + enable_accelerated_networking = false
      + enable_ip_forwarding          = false
      + id                            = (known after apply)
      + internal_dns_name_label       = (known after apply)
      + internal_domain_name_suffix   = (known after apply)
      + location                      = "eastus"
      + mac_address                   = (known after apply)
      + name                          = "Testvm01-nic"
      + private_ip_address            = (known after apply)
      + private_ip_addresses          = (known after apply)
      + resource_group_name           = "myResourceGroup"
      + virtual_machine_id            = (known after apply)

      + ip_configuration {
          + gateway_load_balancer_frontend_ip_configuration_id = (known after apply)
          + name                                               = "internal"
          + primary                                            = (known after apply)
          + private_ip_address                                 = (known after apply)
          + private_ip_address_allocation                      = "Dynamic"
          + private_ip_address_version                         = "IPv4"
        }
    }

  # azurerm_network_interface.nic_win_jumpbox["02"] will be created
  + resource "azurerm_network_interface" "nic_win_jumpbox" {
      + applied_dns_servers           = (known after apply)
      + dns_servers                   = (known after apply)
      + enable_accelerated_networking = false
      + enable_ip_forwarding          = false
      + id                            = (known after apply)
      + internal_dns_name_label       = (known after apply)
      + internal_domain_name_suffix   = (known after apply)
      + location                      = "eastus"
      + mac_address                   = (known after apply)
      + name                          = "Testvm02-nic"
      + private_ip_address            = (known after apply)
      + private_ip_addresses          = (known after apply)
      + resource_group_name           = "myResourceGroup"
      + virtual_machine_id            = (known after apply)

      + ip_configuration {
          + gateway_load_balancer_frontend_ip_configuration_id = (known after apply)
          + name                                               = "internal"
          + primary                                            = (known after apply)
          + private_ip_address                                 = (known after apply)
          + private_ip_address_allocation                      = "Dynamic"
          + private_ip_address_version                         = "IPv4"
        }
    }

  # azurerm_resource_group.resource_group will be created
  + resource "azurerm_resource_group" "resource_group" {
      + id       = (known after apply)
      + location = "eastus"
      + name     = "myResourceGroup"
      + tags     = {
          + "foo" = "bar"
        }
    }

  # azurerm_windows_virtual_machine.win_jumpbox["01"] will be created
  + resource "azurerm_windows_virtual_machine" "win_jumpbox" {
      + admin_password                                         = (sensitive value)
      + admin_username                                         = "azureadmin"
      + allow_extension_operations                             = true
      + bypass_platform_safety_checks_on_user_schedule_enabled = false
      + computer_name                                          = (known after apply)
      + disk_controller_type                                   = (known after apply)
      + enable_automatic_updates                               = false
      + extensions_time_budget                                 = "PT1H30M"
      + hotpatching_enabled                                    = false
      + id                                                     = (known after apply)
      + location                                               = "eastus"
      + max_bid_price                                          = -1
      + name                                                   = "Testvm01"
      + network_interface_ids                                  = (known after apply)
      + patch_assessment_mode                                  = "ImageDefault"
      + patch_mode                                             = "AutomaticByPlatform"
      + platform_fault_domain                                  = -1
      + priority                                               = "Regular"
      + private_ip_address                                     = (known after apply)
      + private_ip_addresses                                   = (known after apply)
      + provision_vm_agent                                     = true
      + public_ip_address                                      = (known after apply)
      + public_ip_addresses                                    = (known after apply)
      + resource_group_name                                    = "myResourceGroup"
      + size                                                   = "Standard_DS1_v2"
      + tags                                                   = {
          + "foo" = "bar"
        }
      + virtual_machine_id                                     = (known after apply)
      + vm_agent_platform_updates_enabled                      = false

      + os_disk {
          + caching                   = "ReadWrite"
          + disk_size_gb              = (known after apply)
          + name                      = "Testvm01"
          + storage_account_type      = "StandardSSD_LRS"
          + write_accelerator_enabled = false
        }

      + source_image_reference {
          + offer     = "WindowsServer"
          + publisher = "MicrosoftWindowsServer"
          + sku       = "2022-datacenter-azure-edition-hotpatch"
          + version   = "latest"
        }
    }

  # azurerm_windows_virtual_machine.win_jumpbox["02"] will be created
  + resource "azurerm_windows_virtual_machine" "win_jumpbox" {
      + admin_password                                         = (sensitive value)
      + admin_username                                         = "azureadmin"
      + allow_extension_operations                             = true
      + bypass_platform_safety_checks_on_user_schedule_enabled = false
      + computer_name                                          = (known after apply)
      + disk_controller_type                                   = (known after apply)
      + enable_automatic_updates                               = false
      + extensions_time_budget                                 = "PT1H30M"
      + hotpatching_enabled                                    = false
      + id                                                     = (known after apply)
      + location                                               = "eastus"
      + max_bid_price                                          = -1
      + name                                                   = "Testvm02"
      + network_interface_ids                                  = (known after apply)
      + patch_assessment_mode                                  = "ImageDefault"
      + patch_mode                                             = "AutomaticByPlatform"
      + platform_fault_domain                                  = -1
      + priority                                               = "Regular"
      + private_ip_address                                     = (known after apply)
      + private_ip_addresses                                   = (known after apply)
      + provision_vm_agent                                     = true
      + public_ip_address                                      = (known after apply)
      + public_ip_addresses                                    = (known after apply)
      + resource_group_name                                    = "myResourceGroup"
      + size                                                   = "Standard_DS1_v2"
      + tags                                                   = {
          + "foo" = "bar"
        }
      + virtual_machine_id                                     = (known after apply)
      + vm_agent_platform_updates_enabled                      = false

      + os_disk {
          + caching                   = "ReadWrite"
          + disk_size_gb              = (known after apply)
          + name                      = "Testvm02"
          + storage_account_type      = "StandardSSD_LRS"
          + write_accelerator_enabled = false
        }

      + source_image_reference {
          + offer     = "WindowsServer"
          + publisher = "MicrosoftWindowsServer"
          + sku       = "2022-datacenter-azure-edition-hotpatch"
          + version   = "latest"
        }
    }

Plan: 5 to add, 0 to change, 0 to destroy.
© www.soinside.com 2019 - 2024. All rights reserved.