使用具有 for_each 集的模块的输出值 [英] Use output value from module that has a for_each set
问题描述
我的代码设置为在创建 VM 时导出动态私有 IP 地址.我通过输出值做到了这一点.从那时起,我已更新到 tf 0.13,并且在模块中使用了 for_each,但是当我现在引用此值时,出现以下错误.现在 for_each 已设置为在 source_address_prefixes 中使用,我不确定如何导出 NIC 的动态私有地址属性.我明白错误的含义,但不确定将值导出到对象映射的正确方法?
I had my code setup to export the dynamic private ip address when the VM is created. I did this via an outputs value. Since then, I have updated to tf 0.13 and I'm using a for_each in the module but when I reference this value now I get the below error. I'm not sure how I can export the dynamic private address attribute of the NIC now the for_each has been set to be used in the source_address_prefixes. I understand what the error is saying but not sure on correct way of exporting the value to the object map?
Error: Unsupported attribute
on main.tf line 66, in resource "azurerm_network_security_rule" "fico-app-sr-80":
66: source_address_prefixes = module.fico_web_vm.linux_vm_ips[0]
|----------------
| module.fico_web_vm is object with 1 attribute "web-1"
This object does not have an attribute named "linux_vm_ips".
outputs.tf 文件:
The outputs.tf file:
output "linux_vm_names" {
value = [azurerm_linux_virtual_machine.virtual_machine.*.name]
}
output "linux_vm_ips" {
value = [azurerm_network_interface.network_interface.*.private_ip_address]
}
output "linux_vm_nsg" {
value = azurerm_network_security_group.network_security_group.name
}
错误所引用的安全规则,位于 main.tf 中:
Security rule where the error is referencing, located in the main.tf:
resource "azurerm_network_security_rule" "fico-app-sr-80" {
name = "nsr-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}${var.instance_number}-http80"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "80"
source_address_prefixes = module.fico_web_vm.linux_vm_ips[0]
destination_address_prefix = "VirtualNetwork"
resource_group_name = azurerm_resource_group.rg_dwp_fico_app.name
network_security_group_name = "module.fico_app_vm.linux_vm_nsg"
}
根模块中的网卡:
resource "azurerm_network_interface" "network_interface" {
name = "nic-${var.environment}-${var.directorate}-${var.business_unit}-${var.vm_identifier}-${var.vm_name}"
resource_group_name = var.resource_group
location = var.location
enable_ip_forwarding = "false"
enable_accelerated_networking = "false"
ip_configuration {
name = "ipconfig1"
subnet_id = data.azurerm_subnet.dwp_subnet.id
private_ip_address_allocation = "Dynamic"
primary = "true"
}
在 main.tf 中构建 web_vm 的模块:
The module that builds the web_vm in main.tf:
module "fico_web_vm" {
for_each = var.web_servers
source = "../modules/compute/linux_vm"
source_image_id = var.web_image_id
location = var.location
vm_name = each.key
vm_identifier = "${var.vm_identifier}${var.instance_number}"
vm = each.value
disks = each.value["disks"]
resource_group = azurerm_resource_group.rg_dwp_fico_web.name
directorate = var.directorate
business_unit = var.business_unit
environment = var.environment
network_rg_identifier = var.network_rg_identifier
subnet_name = "sub-dwp-${var.environment}-${var.directorate}-${var.business_unit}-fe01"
diag_storage_account_name = var.diag_storage_account_name
ansible_storage_account_name = var.ansible_storage_account_name
ansible_storage_account_key = var.ansible_storage_account_key
log_analytics_workspace_name = var.log_analytics_workspace_name
backup_policy_name = var.backup_policy_name
}
main.tf 中应用的变量:
variables for the app in the main.tf:
# Web VM Variables
variable "web_servers" {
description = "Variable for defining each instance"
type = map(object({
size = string
admin_username = string
public_key = string
disks = list(number)
zone_vm = string
zone_disk = list(string)
}))
}
variables.tf 转换到的 tfvars 文件以及 for_each 循环:
The tfvars file that the variables.tf translates to and which the for_each loops over:
web_servers ={
web-1 = {
size = "Standard_B2s"
admin_username = xxxx
public_key = xxxx
disks = [32, 32]
zone_vm = "1"
zone_disk = ["1"]
}
}
推荐答案
据我所知,您的代码中有两个错误.
As I see, there two mistakes in your code.
首先:
network_security_group_name = "module.fico_app_vm.linux_vm_nsg"
需要改成:
network_security_group_name = module.fico_app_vm.linux_vm_nsg
双引号通常用于设置字符串,仅当您使用它们时,而不是其他资源.
The double quotes are usually used to set a string when you use them only, not other resources.
第二:
source_address_prefixes = module.fico_web_vm.linux_vm_ips[0]
需要改成:
source_address_prefixes = module.fico_web_vm.linux_vm_ips
并且输出也应该变成:
output "linux_vm_ips" {
value = azurerm_network_interface.network_interface.*.private_ip_address
}
我看到您只创建了一个没有 count
和 for_each
的 NIC,但是当您使用 azurerm_network_interface.network_interface.*.private_ip_address
设置值时然后它也返回一个列表.而 source_address_prefixes
选项需要一个列表,所以你只需要给出输出.
I see you just create one NIC without count
and for_each
, but when you set the value with azurerm_network_interface.network_interface.*.private_ip_address
then it also returns a list. And the source_address_prefixes
option expects a list, so you just need to give the output.
也许像这样更改输出会更好:
Maybe it's better to change the output like this:
output "linux_vm_ip" {
value = azurerm_network_interface.network_interface.private_ip_address
}
然后你可以像这样设置source_address_prefixes
:
And then you can set the source_address_prefixes
like this:
source_address_prefixes = [ module.fico_web_vm.linux_vm_ip ]
这篇关于使用具有 for_each 集的模块的输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!