如何从从地图中提取的列表中创建多个动态块条目

how to create multiple dynamic block entries from a list pulled from a map

我有一个地图变量

variable ltm-datagroups = {
  "0" = {
    datagroup_name      = "abc"
    datagroup_type      = "ip"
    datagroup_addresses = ["10.0.0.0/8", "172.16.1.0/24"]
  }
  "1" = {
    datagroup_name      = "def"
    datagroup_type      = "ip"
    datagroup_addresses = ""
  }
}

我将此映射传递给一个模块,该模块查找“datagroup_addresses”的值。我想根据列表的长度创建多个条目。

resource "bigip_ltm_datagroup" "datagroup" {
  for_each = var.ltm-datagroups
  name     = lookup(var.ltm-datagroups[each.key], "datagroup_name")
  type     = lookup(var.ltm-datagroups[each.key], "datagroup_type")
  dynamic "record" {
    for_each = lookup(var.ltm-datagroups[each.key], "datagroup_addresses") != "" ? ["${length(lookup(var.ltm-datagroups[each.key], "datagroup_addresses"))}"] : []
    content {
      name = lookup(var.ltm-datagroups[each.key], "datagroup_addresses")
    }
  }
}

这是我看到的错误

Error: Incorrect attribute value type

  on modules/ltm-datagroup/main.tf line 8, in resource "bigip_ltm_datagroup" "datagroup":
   8:   name = lookup(var.ltm-datagroups[each.key], "datagroup_addresses")
    |----------------
    | each.key is "0"
    | var.ltm-datagroups is object with 2 attributes

Inappropriate value for attribute "name": string required.


Error: Incorrect attribute value type

  on modules/ltm-datagroup/main.tf line 8, in resource "bigip_ltm_datagroup" "datagroup":
   8:   name = lookup(var.ltm-datagroups[each.key], "datagroup_addresses")
    |----------------
    | each.key is "1"
    | var.ltm-datagroups is object with 2 attributes

Inappropriate value for attribute "name": string required.

我卡在了最后一部分。如何运行动态块多次?在遍历列表中的条目时?

不确定我是否完全理解您想要的结果,但如果您想动态创建 record,那么它应该是:

  dynamic "record" {
    for_each = lookup(var.ltm-datagroups[each.key], "datagroup_addresses") != "" ? toset(lookup(var.ltm-datagroups[each.key], "datagroup_addresses")) : []
    content {
      name = record.value
    }
  }