【发布时间】:2021-03-12 14:32:20
【问题描述】:
所以我开始探索 Terraform,到目前为止,我能够在 Azure 中启动我的网络和虚拟机。 由于我创建了 7 个 VM,因此我想获取它的 IP 地址和主机名。
在我的 main.tf 我有这个:
provider "azurerm" {
features {}
# Configuration options
}
module "splunk_architect" {
source = "./modules/architect"
}
只是我在 /modules/architect 中的 main.tf 中的一个示例
resource "azurerm_network_interface" "main" {
for_each = toset(var.vm_names)
name = "${each.value}-nic"
location = azurerm_resource_group.rsg.location
resource_group_name = azurerm_resource_group.rsg.name
ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pubip[each.key].id
}
还有我在 /modules/architect 中的 outputs.tf
output "ip" {
value = azurerm_network_interface.main[each.key].private_ip_address
}
所以当我运行它时,我收到以下错误消息:
Error: Reference to "each" in context without for_each
on modules\architect\outputs.tf line 6, in output "ip": 6: value = azurerm_network_interface.main[each.key].private_ip_address
The "each" object can be used only in "module" or "resource" blocks, and only when the "for_each" argument is set.
所以我尝试在模块中设置 for_each,但没有让它工作。 我也通过了文件没有任何成功。 关于我可以尝试打印出每个 VM 的 IP 的任何提示?
【问题讨论】: