【发布时间】:2022-01-14 16:40:14
【问题描述】:
我有这样的变量:
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_linux_virtual_machine" "虚拟机?