如何 for_each 在地图中使用基于工作空间的字符串
How to for_each over a map inside a map with strings based on workspace
我有这样的变量:
variable "hosts" {
type = map(map(string))
default = {
workspace1 = {
hostname1-workspace1-1 = 177.104
hostname1-workspace1-2 = 177.105
hostname1-workspace1-3 = 177.106
}
workspace2 = {
hostname1-workspace2-1 = 129.124
hostname1-workspace2-2 = 129.125
hostname1-workspace2-3 = 129.126
}
}
}
并且我正在尝试 for_each 遍历此映射,因此我可以为映射中的每个主机创建一个 VM NIC,但仅限于选择的任何工作空间。
其中 each.key 应该是当前所选工作区的名称,each.value 应该是基于工作区的 IP 地址的最后 2 个八位字节我试过这样的事情:
resource "azurerm_network_interface" "redis_vm_nic" {
for_each = var.hosts[terraform.workspace]
name = "${each.key}VMNic"
location = "${var.redis_rg_location[terraform.workspace]}"
resource_group_name = azurerm_resource_group.infrastructure_redis_rg.name
ip_configuration {
name = "ipconfig${each.key}"
subnet_id = var.aks_subnet
private_ip_address_allocation = "Static"
private_ip_address = "10.99.${each.value}"
}
tags = var.tags_vms
}
但我得到:
│
│ on main.tf line 59, in resource "azurerm_linux_virtual_machine" "virtual_machine":
│ 59: network_interface_ids = [azurerm_network_interface.redis_vm_nic[each.key].id]
│ ├────────────────
│ │ azurerm_network_interface.redis_vm_nic is object with 3 attributes
│ │ each.key is "workspace1"
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on main.tf line 59, in resource "azurerm_linux_virtual_machine" "virtual_machine":
│ 59: network_interface_ids = [azurerm_network_interface.redis_vm_nic[each.key].id]
│ ├────────────────
│ │ azurerm_network_interface.redis_vm_nic is object with 3 attributes
│ │ each.key is "workspace2"
│
│ The given key does not identify an element in this collection value.
╵
你的问题肯定不在azurerm_network_interface
。您在子地图上正确迭代。我认为问题在于您如何在 azurerm_linux_virtual_machine
中执行 for_each
。根据我在错误中看到的内容,Linux VM 的 for_each
可能采用以下格式:
for_each = var.hosts
您最想拥有 azurerm_network_interface
中的原样,所以:
resource "azurerm_linux_virtual_machine" "virtual_machine" {
for_each = var.hosts[terraform.workspace]
...
}
我有这样的变量:
variable "hosts" {
type = map(map(string))
default = {
workspace1 = {
hostname1-workspace1-1 = 177.104
hostname1-workspace1-2 = 177.105
hostname1-workspace1-3 = 177.106
}
workspace2 = {
hostname1-workspace2-1 = 129.124
hostname1-workspace2-2 = 129.125
hostname1-workspace2-3 = 129.126
}
}
}
并且我正在尝试 for_each 遍历此映射,因此我可以为映射中的每个主机创建一个 VM NIC,但仅限于选择的任何工作空间。 其中 each.key 应该是当前所选工作区的名称,each.value 应该是基于工作区的 IP 地址的最后 2 个八位字节我试过这样的事情:
resource "azurerm_network_interface" "redis_vm_nic" {
for_each = var.hosts[terraform.workspace]
name = "${each.key}VMNic"
location = "${var.redis_rg_location[terraform.workspace]}"
resource_group_name = azurerm_resource_group.infrastructure_redis_rg.name
ip_configuration {
name = "ipconfig${each.key}"
subnet_id = var.aks_subnet
private_ip_address_allocation = "Static"
private_ip_address = "10.99.${each.value}"
}
tags = var.tags_vms
}
但我得到:
│
│ on main.tf line 59, in resource "azurerm_linux_virtual_machine" "virtual_machine":
│ 59: network_interface_ids = [azurerm_network_interface.redis_vm_nic[each.key].id]
│ ├────────────────
│ │ azurerm_network_interface.redis_vm_nic is object with 3 attributes
│ │ each.key is "workspace1"
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on main.tf line 59, in resource "azurerm_linux_virtual_machine" "virtual_machine":
│ 59: network_interface_ids = [azurerm_network_interface.redis_vm_nic[each.key].id]
│ ├────────────────
│ │ azurerm_network_interface.redis_vm_nic is object with 3 attributes
│ │ each.key is "workspace2"
│
│ The given key does not identify an element in this collection value.
╵
你的问题肯定不在azurerm_network_interface
。您在子地图上正确迭代。我认为问题在于您如何在 azurerm_linux_virtual_machine
中执行 for_each
。根据我在错误中看到的内容,Linux VM 的 for_each
可能采用以下格式:
for_each = var.hosts
您最想拥有 azurerm_network_interface
中的原样,所以:
resource "azurerm_linux_virtual_machine" "virtual_machine" {
for_each = var.hosts[terraform.workspace]
...
}