如何从地图中的地图输出特定属性,同时在 terraform 中将属性子字符串作为条件?

问题描述 投票:0回答:1

目标:

使用 Terraform 1.3.5 和最新的 AzureRM 提供程序版本,将(动态)子网 ID 分配给 VM NIC,但仅当子网名称 =“all”时

主要前提:

  • 子网名称是另一个映射(对象({}))变量中映射的属性(下面的完整输出)

这个:

output "debug_all_subnet_id" {
  value = azurerm_virtual_network.vnet_workloads
}

产生这个:

Changes to Outputs:
  + debug_all_subnet_id = {
      + customerA = {
          + address_space           = [
              + "10.200.17.128/28",
            ]
          + bgp_community           = null
          + ddos_protection_plan    = []
          + dns_servers             = (known after apply)
          + edge_zone               = null
          + encryption              = []
          + flow_timeout_in_minutes = null
          + guid                    = (known after apply)
          + id                      = (known after apply)
          + location                = "westeurope"
          + name                    = (known after apply)
          + resource_group_name     = (known after apply)
          + subnet                  = [
              + {
                  + address_prefix = "10.200.17.128/29"
                  + id             = (known after apply)
                  + name           = "base"
                  + security_group = ""
                },
              + {
                  + address_prefix = "10.200.17.136/29"
                  + id             = (known after apply)
                  + name           = "all"
                  + security_group = ""
                },
            ]
          + tags                    = null
          + timeouts                = null
        }
      + customerB = {
          + address_space           = [
              + "10.200.17.192/28",
            ]
          + bgp_community           = null
          + ddos_protection_plan    = []
          + dns_servers             = (known after apply)
          + edge_zone               = null
          + encryption              = []
          + flow_timeout_in_minutes = null
          + guid                    = (known after apply)
          + id                      = (known after apply)
          + location                = "eastus2"
          + name                    = (known after apply)
          + resource_group_name     = (known after apply)
          + subnet                  = [
              + {
                  + address_prefix = "10.200.17.192/29"
                  + id             = (known after apply)
                  + name           = "base"
                  + security_group = ""
                },
              + {
                  + address_prefix = "10.200.17.200/29"
                  + id             = (known after apply)
                  + name           = "all"
                  + security_group = ""
                },
            ]
          + tags                    = null
          + timeouts                = null
        }
      + hubA      = {
          + address_space           = [
              + "10.200.17.0/27",
            ]
          + bgp_community           = null
          + ddos_protection_plan    = []
          + dns_servers             = (known after apply)
          + edge_zone               = null
          + encryption              = []
          + flow_timeout_in_minutes = null
          + guid                    = (known after apply)
          + id                      = (known after apply)
          + location                = "westeurope"
          + name                    = (known after apply)
          + resource_group_name     = (known after apply)
          + subnet                  = [
              + {
                  + address_prefix = "10.200.17.0/28"
                  + id             = (known after apply)
                  + name           = "base"
                  + security_group = ""
                },
              + {
                  + address_prefix = "10.200.17.16/28"
                  + id             = (known after apply)
                  + name           = "all"
                  + security_group = ""
                },
            ]
          + tags                    = null
          + timeouts                = null
        }
      + hubB      = {
          + address_space           = [
              + "10.200.17.64/27",
            ]
          + bgp_community           = null
          + ddos_protection_plan    = []
          + dns_servers             = (known after apply)
          + edge_zone               = null
          + encryption              = []
          + flow_timeout_in_minutes = null
          + guid                    = (known after apply)
          + id                      = (known after apply)
          + location                = "eastus2"
          + name                    = (known after apply)
          + resource_group_name     = (known after apply)
          + subnet                  = [
              + {
                  + address_prefix = "10.200.17.64/28"
                  + id             = (known after apply)
                  + name           = "base"
                  + security_group = ""
                },
              + {
                  + address_prefix = "10.200.17.80/28"
                  + id             = (known after apply)
                  + name           = "all"
                  + security_group = ""
                },
            ]
          + tags                    = null
          + timeouts                = null
        }
    }

我正在尝试做这样的事情,但不知道如何用 terraform 来实现它:

output "debug_all_subnet_id" {

  if azurerm_virtual_network.vnet_workloads[*].subnet.name = "all"
   then
    value = azurerm_virtual_network.vnet_workloads[*].subnet.id
  else
   exit/null

}

旁注:

这个:

output "debug_all_subnet_id" {
  value = azurerm_virtual_network.vnet_workloads["customerA"].subnet
}

给了我这个,这让我更接近所需的输出,但还不够接近,因为在主代码中我需要检索不同子网名称(“all”/“snet-all”)的ID vnet 和子网:

Changes to Outputs:
  + debug_all_subnet_id = [
      + {
          + address_prefix = "10.200.17.128/29"
          + id             = (known after apply)
          + name           = "base"
          + security_group = ""
        },
      + {
          + address_prefix = "10.200.17.136/29"
          + id             = (known after apply)
          + name           = "all"
          + security_group = ""
        },
    ]

有什么想法吗?

azure terraform tuples terraform-provider-azure
1个回答
0
投票

我会尝试给出答案,但请记住,这是将您显示的输出移动到局部变量,因此您可以尝试根据您的需要进行调整:

locals {
  debug_all_subnet_id = {
    customerA = {
      address_space = [
        "10.200.17.128/28",
      ]
      bgp_community           = null
      ddos_protection_plan    = []
      dns_servers             = ""
      edge_zone               = null
      encryption              = []
      flow_timeout_in_minutes = null
      guid                    = ""
      id                      = ""
      location                = "westeurope"
      name                    = ""
      resource_group_name     = ""
      subnet = [
        {
          address_prefix = "10.200.17.128/29"
          id             = ""
          name           = "base"
          security_group = ""
        },
        {
          address_prefix = "10.200.17.136/29"
          id             = ""
          name           = "all"
          security_group = ""
        },
      ]
      tags     = null
      timeouts = null
    }
    customerB = {
      address_space = [
        "10.200.17.192/28",
      ]
      bgp_community           = null
      ddos_protection_plan    = []
      dns_servers             = ""
      edge_zone               = null
      encryption              = []
      flow_timeout_in_minutes = null
      guid                    = ""
      id                      = ""
      location                = "eastus2"
      name                    = ""
      resource_group_name     = ""
      subnet = [
        {
          address_prefix = "10.200.17.192/29"
          id             = ""
          name           = "base"
          security_group = ""
        },
        {
          address_prefix = "10.200.17.200/29"
          id             = ""
          name           = "all"
          security_group = ""
        },
      ]
      tags     = null
      timeouts = null
    }
    hubA = {
      address_space = [
        "10.200.17.0/27",
      ]
      bgp_community           = null
      ddos_protection_plan    = []
      dns_servers             = ""
      edge_zone               = null
      encryption              = []
      flow_timeout_in_minutes = null
      guid                    = ""
      id                      = ""
      location                = "westeurope"
      name                    = ""
      resource_group_name     = ""
      subnet = [
        {
          address_prefix = "10.200.17.0/28"
          id             = ""
          name           = "base"
          security_group = ""
        },
        {
          address_prefix = "10.200.17.16/28"
          id             = ""
          name           = "all"
          security_group = ""
        },
      ]
      tags     = null
      timeouts = null
    }
    hubB = {
      address_space = [
        "10.200.17.64/27",
      ]
      bgp_community           = null
      ddos_protection_plan    = []
      dns_servers             = ""
      edge_zone               = null
      encryption              = []
      flow_timeout_in_minutes = null
      guid                    = ""
      id                      = ""
      location                = "eastus2"
      name                    = ""
      resource_group_name     = ""
      subnet = [
        {
          address_prefix = "10.200.17.64/28"
          id             = ""
          name           = "base"
          security_group = ""
        },
        {
          address_prefix = "10.200.17.80/28"
          id             = ""
          name           = "all"
          security_group = ""
        },
      ]
      tags     = null
      timeouts = null
    }
}

output "debug_all_subnet_id" {
  value = flatten([for i in values(local.debug_all_subnet_id)[*].subnet: [ for s in i: s if s.name == "all" ]])
}

这将导致:

> flatten([for i in values(local.debug_all_subnet_id)[*].subnet: [ for s in i: s if s.name == "all" ]])
[
  {
    "address_prefix" = "10.200.17.136/29"
    "id" = ""
    "name" = "all"
    "security_group" = ""
  },
  {
    "address_prefix" = "10.200.17.200/29"
    "id" = ""
    "name" = "all"
    "security_group" = ""
  },
  {
    "address_prefix" = "10.200.17.16/28"
    "id" = ""
    "name" = "all"
    "security_group" = ""
  },
  {
    "address_prefix" = "10.200.17.80/28"
    "id" = ""
    "name" = "all"
    "security_group" = ""
  },
]
© www.soinside.com 2019 - 2024. All rights reserved.