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



我设置了代码,以便在创建VM时导出动态私有ip地址。我是通过一个输出值来做的。从那时起,我已经更新到tf 0.13,我在模块中使用for_each,但是当我引用这个值时,现在我得到下面的错误。我不确定如何导出网卡的动态私有地址属性,现在for_each已被设置为在source_address_prefixes中使用。我明白错误是什么意思,但不确定导出值到对象映射的正确方式?

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

输出。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
}

错误引用的安全规则,位于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的模块
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:

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

包含变量的tfvars文件。Tf转换为for_each循环遍历的对象:

web_servers ={
  web-1 = {
    size           = "Standard_B2s"
    admin_username = xxxx
    public_key     = xxxx
    disks          = [32, 32]
    zone_vm        = "1"
    zone_disk      = ["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的网卡,但是当你用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 ]

最新更新