1

我目前正在努力解决如何在地图列表中搜索地图列表。

locals {

  vpn_configurations = [

    {
      customer_name      = "test125231"
      custom_path        = "test123123"
      shared_by          = []
      nat                = false
      nat_source_ip      = ""
      nat_destination_ip = ""
      nat_route          = ""
      tunnels = [
        {
          tunnel_name   = "test-tunnel"
          left          = "%defaultroute"
          leftid        = ""
          leftsubnet    = ""
          leftsourceip  = ""
          rightid       = ""
          right         = ""
          rightsubnet   = ""
          rightsourceip = ""
          ike           = "aes256-sha256-modp2048"
          keyexchange   = "ike"
          ikev2         = "no"
          esp           = "aes256-sha256-modp2048"
          salifetime    = 3600
          ikelifetime   = 3600
          authby        = "secret"
          # use auto=start when done testing the tunnel
          auto = "ondemand"
        },
        {
          tunnel_name   = "test-tunnel2"
          left          = "%defaultroute"
          leftid        = ""
          leftsubnet    = ""
          leftsourceip  = ""
          rightid       = ""
          right         = ""
          rightsubnet   = ""
          rightsourceip = ""
          ike           = "aes256-sha256-modp2048"
          keyexchange   = "ike"
          ikev2         = "no"
          esp           = "aes256-sha256-modp2048"
          salifetime    = 3600
          ikelifetime   = 3600
          authby        = "secret"
          # use auto=start when done testing the tunnel
          auto = "ondemand"
        }
      ]
    },
    {
      customer_name      = "sdfsdfsd"
      custom_path        = "sdfsdfsdf"
      shared_by          = []
      nat                = false
      nat_source_ip      = ""
      nat_destination_ip = ""
      nat_route          = ""
      tunnels = [
        {
          tunnel_name   = "test-tunnel3"
          left          = "%defaultroute"
          leftid        = ""
          leftsubnet    = ""
          leftsourceip  = ""
          rightid       = ""
          right         = ""
          rightsubnet   = ""
          rightsourceip = ""
          ike           = "aes256-sha256-modp2048"
          keyexchange   = "ike"
          ikev2         = "no"
          esp           = "aes256-sha256-modp2048"
          salifetime    = 3600
          ikelifetime   = 3600
          authby        = "secret"
          # use auto=start when done testing the tunnel
          auto = "ondemand"
        },
        {
          tunnel_name   = "test-tunnel4"
          left          = "%defaultroute"
          leftid        = ""
          leftsubnet    = ""
          leftsourceip  = ""
          rightid       = ""
          right         = ""
          rightsubnet   = ""
          rightsourceip = ""
          ike           = "aes256-sha256-modp2048"
          keyexchange   = "ike"
          ikev2         = "no"
          esp           = "aes256-sha256-modp2048"
          salifetime    = 3600
          ikelifetime   = 3600
          authby        = "secret"
          # use auto=start when done testing the tunnel
          auto = "ondemand"
        }
      ]
    }

  ]

}

我正在尝试将每个 vpn 配置的每个隧道传递到 terraform 模板生成器中,以便为与客户关联的每个隧道创建单独的配置文件。

data "template_file" "networking_configs" {
  for_each = local.vpn_configurations
  template = file("${path.module}/template-files/networking-templates/tunnel-configuration.tpl")
  vars = {

    tunnel_name   = each.value.tunnels["tunnel_name"]
    left          = each.value.tunnels["left"]
    leftid        = module.ipsec.public_ip
    leftsubnet    = each.value.tunnels["leftsubnet"]
    leftsourceip  = data.aws_network_interface.eni_ip.private_ip
    rightid       = each.value.tunnels["rightid"]
    right         = each.value.tunnels["right"]
    rightsubnet   = each.value.tunnels["rightsubnet"]
    rightsourceip = each.value.tunnels["rightsourceip"]
    ike           = each.value.tunnels["ike"]
    keyexchange   = each.value.tunnels["keyexchange"]
    ikev2         = each.value.tunnels["ikev2"]
    esp           = each.value.tunnels["esp"]
    salifetime    = each.value.tunnels["salifetime"]
    ikelifetime   = each.value.tunnels["ikelifetime"]
    authby        = each.value.tunnels["authby"]
    auto          = each.value.tunnels["auto"]

  }
}

感谢任何可用的帮助。

4

1 回答 1

0

你必须将你的双 for 循环扁平化为一个。例如(仅列出了一些变量):

locals {
  flat_vpn_configurations = merge([
         for vpn_config in local.vpn_configurations :
         {
             for tunnel in vpn_config["tunnels"]:
             "${vpn_config["customer_name"]}-${tunnel["tunnel_name"]}" =>
              {
                  customer_name = vpn_config["customer_name"]
                  custom_path = vpn_config["custom_path"]
                  tunnel_name = tunnel["tunnel_name"]
                  left = tunnel["left"]
              }
         }
      ]...)
}

这将为您local.flat_vpn_configurations提供(仅显示部分):

{
  "sdfsdfsd-test-tunnel3" = {
    "custom_path" = "sdfsdfsdf"
    "customer_name" = "sdfsdfsd"
    "left" = "%defaultroute"
    "tunnel_name" = "test-tunnel3"
  }
  "sdfsdfsd-test-tunnel4" = {
    "custom_path" = "sdfsdfsdf"
    "customer_name" = "sdfsdfsd"
    "left" = "%defaultroute"
    "tunnel_name" = "test-tunnel4"
  }
  "test125231-test-tunnel" = {
    "custom_path" = "test123123"
    "customer_name" = "test125231"
    "left" = "%defaultroute"
    "tunnel_name" = "test-tunnel"
  }
  "test125231-test-tunnel2" = {
    "custom_path" = "test123123"
    "customer_name" = "test125231"
    "left" = "%defaultroute"
    "tunnel_name" = "test-tunnel2"
  }
}

然后你可以很容易地flat_vpn_configurationsfor_each.

于 2021-10-14T22:48:57.337 回答