使用具有 for_each 集的模块的输出值

时间:2021-01-13 15:15:23

标签: terraform terraform-provider-azure azure-rm

我的代码设置为在创建 VM 时导出动态私有 IP 地址。我通过输出值做到了这一点。从那时起,我已更新到 tf 0.13,并且在模块中使用了 for_each,但是当我现在引用此值时,出现以下错误。现在 for_each 已设置为在 source_address_prefixes 中使用,我不确定如何导出 NIC 的动态私有地址属性。我明白错误的含义,但不确定将值导出到对象映射的正确方法?

Dim x As Workbook
Dim y As Workbook
Dim vals As Variant


Set y= ThisWorkbook
Set x= Workbooks.Open("FileName.xls", CorruptLoad:=XlCorruptLoad.xlRepairFile)

vals = x.Sheets("SheetSource").Range("G10:C10").Value

y.Sheets("Sheet1").Range("G8:G12").Value = WorksheetFunction.Transpose(vals)

outputs.tf 文件:

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".

错误所引用的安全规则,位于 main.tf 中:

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
}

根模块中的网卡:

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"
}

在 main.tf 中构建 web_vm 的模块:

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 中应用的变量:

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
}

variables.tf 转换到的 tfvars 文件以及 for_each 循环:

# 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)
  }))
}

1 个答案:

答案 0 :(得分:1)

如我所见,您的代码中有两个错误。

首先:

network_security_group_name = "module.fico_app_vm.linux_vm_nsg"

需要改成:

network_security_group_name = module.fico_app_vm.linux_vm_nsg

双引号通常用于设置字符串,仅当您使用它们时,而不是其他资源。

第二:

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
}

我看到您只创建了一个没有 countfor_each 的 NIC,但是当您使用 azurerm_network_interface.network_interface.*.private_ip_address 设置值时,它也会返回一个列表。而 source_address_prefixes 选项需要一个列表,因此您只需要提供输出即可。

也许像这样更改输出会更好:

output "linux_vm_ip" {
  value = azurerm_network_interface.network_interface.private_ip_address
}

然后您可以像这样设置 source_address_prefixes

source_address_prefixes     = [ module.fico_web_vm.linux_vm_ip ]