Terraform 0.12 - 从嵌套的 for 循环中生成地图/对象 [英] Terraform 0.12 - Produce map/object from nested for loop

查看:32
本文介绍了Terraform 0.12 - 从嵌套的 for 循环中生成地图/对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 Terraform 0.12 嵌套 for 循环.我试图从嵌套循环中生成一个对象,但失败得很惨:(

你会如何制作:

 输出:关联列表 = {策略 1" = 用户 1"策略 2" = 用户 1"策略 2" = 用户 2"}

发件人:

iam-policy-users-map = {"policy1" = [ "user1" ]"policy2" = [ "user1", "user2" ]}

我尝试了很多变体:

变量 iam-policy-users-map {默认 = {"policy1" = [ "user1" ]"policy2" = [ "user1", "user2" ]}}当地人{关联图=合并({对于策略,var.iam-policy-users-map 中的用户:{对于用户中的用户:{政策=>用户}}})输出关联图 {值 = local.association-map}

到目前为止,成功率为零.仅设法根据变化获得以下内容:

<块引用>

错误:'for' 表达式无效.'for' 表达式末尾的多余字符.

错误:缺少属性值.需要一个属性值,由等号 ("=") 引入.

错误:'for' 表达式无效.构建对象时需要键表达式.

错误:缺少键/值分隔符.应为等号 ("=")标记属性值的开始.

作为参考,以下代码可以生成地图列表:

变量 iam-policy-users-map {默认 = {"policy1" = [ "user1" ]"policy2" = [ "user1", "user2" ]}}当地人{关联列表 = 展平([对于策略,var.iam-policy-users-map 中的用户:[对于用户中的用户:{用户 = 用户政策=政策}]])}输出关联列表 {值 = local.association-list}

<块引用>

输出:

关联列表 = [ {政策" = 政策 1"用户"=用户1"},{政策" = 政策 2"用户"=用户1"},{政策" = 政策 2""用户" = "用户2" }, ]

解决方案

部分答案可以在 https://github.com/hashicorp/terraform/issues/22263.长话短说:这是一个愚蠢的尝试,地图不能包含重复的键.

不过,我仍然有兴趣了解如何从嵌套的 for 循环生成地图地图.请参阅上面的第二个代码示例,生成地图列表.

在上面链接的 github 问题上给出了完整的答案.

这(显然)是一个无用的结构,但我想说明它是可能的:

本地人{关联列表 = {对于策略,var.iam-policy-users-map 中的用户:政策=>{//键之前不能有嵌套的 for 表达式!对于你的用户:政策=>你...}}}输出:关联列表 = {政策1"= {政策1"= [用户1",]}政策2"= {政策2"= [用户1",用户2",]}}

"

As a follow up to Terraform 0.12 nested for loops. I am trying to produce an object out of a nested loop but failing miserably :(

How would you go about producing:

  Outputs:

  association-list = {
    "policy1" = "user1"
    "policy2" = "user1"
    "policy2" = "user2"
  }

From:

iam-policy-users-map = {
  "policy1" = [ "user1" ]
  "policy2" = [ "user1", "user2" ]
}

I have tried many variations of:

variable iam-policy-users-map {
  default = {
    "policy1" = [ "user1" ]
    "policy2" = [ "user1", "user2" ]
  }
}

locals {
  association-map = merge({
    for policy, users in var.iam-policy-users-map : {
      for user in users : {
        policy => user
      }
    }
  })

output association-map {
  value = local.association-map
}

with zero success so far. Only managed to get the following depending on the variation:

Error: Invalid 'for' expression. Extra characters after the end of the 'for' expression.

Error: Missing attribute value. Expected an attribute value, introduced by an equals sign ("=").

Error: Invalid 'for' expression. Key expression is required when building an object.

Error: Missing key/value separator. Expected an equals sign ("=") to mark the beginning of the attribute value.

For reference, the following code is however capable of producing a list of maps:

variable iam-policy-users-map {
  default = {
    "policy1" = [ "user1" ]
    "policy2" = [ "user1", "user2" ]
  }
}

locals {    
  association-list = flatten([
    for policy, users in var.iam-policy-users-map : [
      for user in users : {
        user   = user
        policy = policy
      }
    ]
  ])
}

output association-list {
  value = local.association-list
}

Outputs:

association-list = [ { "policy" = "policy1" "user" = "user1" }, { "policy" = "policy2" "user" = "user1" }, { "policy" = "policy2" "user" = "user2" }, ]

解决方案

A partial answer can be found at https://github.com/hashicorp/terraform/issues/22263. Long story short: this was a foolish attempt to begin with, a map cannot contain duplicate keys.

I am however still interested in understanding how a map of maps could be produced from a nested for loop. See second code example above, producing a list of maps.

EDIT: a full answer was given on the github issue linked above.

"This is (obviously) a useless structure, but I wanted to illustrate that it is possible:

locals {
  association-list = {
    for policy, users in var.iam-policy-users-map:
      policy => {      // can't have the nested for expression before the key!
        for u in users:
           policy => u...
      }
  }
}

Outputs:

association-list = {
  "policy1" = {
    "policy1" = [
      "user1",
    ]
  }
  "policy2" = {
    "policy2" = [
      "user1",
      "user2",
    ]
  }
}

"

这篇关于Terraform 0.12 - 从嵌套的 for 循环中生成地图/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆