如何在 terraform avi gslb 中实现 for_each 以创建多个池并将其附加到 gslb?

how to implement for_each in terraform avi gslb to create and attach multiple pool to gslb?

我在下面创建了一个创建并附加到它的池的 avi gslbservice。我想创建第二个创建并附加到它的池。任何人都可以指导吗? 我是 terraform 的新手,我很少看到关于 for_each fn 的教程。但无法弄清楚如何根据我的需要应用它。 我已经突出显示了创建 gslb 池的块

resource "avi_gslbservice" "avi_gslbservice" {
name = "helloworldssl-gslb"
tenant_ref = data.avi_tenant.avi_tenant.id
domain_names = ["xxxxxxxxx"]
health_monitor_refs = [avi_healthmonitor.avi_healthmonitor_gslb.id]
enabled = true
pool_algorithm = "GSLB_SERVICE_ALGORITHM_GEO"
ttl = "30"
created_by = "xxxxxx"
description = "xxxxxx"
down_response {
type = "GSLB_SERVICE_DOWN_RESPONSE_ALL_RECORDS"
             }
**groups {
      priority = 10
      members {
              ip {
                type = "V4"
                addr = ""
                 }
         fqdn = "xxxxxxxxxxxxxx"
         vs_uuid = ""
         cluster_uuid = ""
         ratio = 1
         enabled = true
         }              
      name = "helloworldssl-gslb-pool1"
      algorithm = "GSLB_ALGORITHM_TOPOLOGY"
      }**
}

编辑 2021 年 8 月 8 日 - 现在我有一个解决方法,将整个组块复制两次。

这是你的做法,

dynamic "groups" {   
   for_each = var.avi_gslbservice_groups
   content {
      dynamic "members" {
        for_each = groups.value.avi_gslbservice_groups_ip
        content {
            ip {
                 type = "V4"
                 addr = ""
              }
              fqdn = members.value["host"]
              vs_uuid = ""
              cluster_uuid = ""
              ratio = 1
              enabled = members.value["enabled"]
            }         
          }                 
        name = groups.value["name"]
        priority = groups.value["priority"]
        algorithm = groups.value["algorithm"]
      }
     }

值将来自 json 文件,如下所示,

{
    "avi_gslbservice_groups": [
        {
            "name": "us-east-1",
            "priority": 7,
            "algorithm": "GSLB_ALGORITHM_ROUND_ROBIN",
            "avi_gslbservice_groups_ip": [
                {
                    "host": "host1",
                    "enabled": "true"
                },
                {
                    "host": "host2",
                    "enabled": "false"
                }
            ]
        },
        {
            "name": "us-east-2",
            "priority": 10,
            "algorithm": "GSLB_ALGORITHM_TOPOLOGY",
            "avi_gslbservice_groups_ip": [
                {
                    "host": "host1",
                    "enabled": "true"
                },
                {
                    "host": "host2",
                    "enabled": "false"
                }
            ]
        }
    ]
}