代码之家  ›  专栏  ›  技术社区  ›  aa-sum

Terraform:如何迭代对象中的多个值

  •  0
  • aa-sum  · 技术社区  · 3 年前

    我正在尝试创建一个带有名称和描述的资源。两者都是弦

    变量。TF

    变量“leaf_int_prof_group”{

    type = object({
        name = string
        description = string
    })
    

    }

    地形。TFVARS

    leaf\u int\u prof\u组={

    {
       name = "Leaf_101"
       description = "This is a Leaf101"
    }
    {
       name = "Leaf_102"
       description = "This is a Leaf102"
    }
    {
       name = "Leaf_103"
       description = "This is a Leaf103"
    }
    

    }

    主要的TF

    我将如何定义main中的变量。tf(以下似乎不起作用??)

    资源“aci_叶_接口_配置文件”“LN_组”{

    for_each = var.leaf_int_prof_group
        name = var.leaf_int_prof_group.name
        description = var.leaf_int_prof_group.description
    

    }

    1 回复  |  直到 3 年前
        1
  •  1
  •   Marcin    3 年前

    您的代码不是有效的TF代码。也许您想要一个对象列表:

    variable "leaf_int_prof_group"{
        type = list(object({
            name = string
            description = string
        })) 
    }
    

    然后

    leaf_int_prof_group =  [
        {
           name = "Leaf_101"
           description = "This is a Leaf101"
        },
        {
           name = "Leaf_102"
           description = "This is a Leaf102"
        },
        {
           name = "Leaf_103"
           description = "This is a Leaf103"
        }   
    ]
    

    最后:

    resource "some_resource" "some_name" {
        for_each    = {for idx, val in var.leaf_int_prof_group: idx => val}
        name        = each.value.name
        description = each.value.description
    }