本文介绍了在 Terraform 中将列表转换为带有索引的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 terraform 中的简单字符串列表转换为以键为索引的地图.

I would like to convert a simple list of string in terraform to a map with the keys as indexes.

我想从这样的事情出发:

I want to go from something like this:

locals {
  keycloak_secret = [
    "account-console",
    "admin-cli",
    "broker",
    "internal",
    "realm-management",
    "security-admin-console",
  ]
}

类似

map({0:"account-console", 1:"admin-cli"}, ...) 

我的目标是利用 terraform 0.13 的新功能在 terraform 模块.

My goal is to take advantage of the new functionality of terraform 0.13 to use loop over map on terraform module.

我没有找到任何解决方案,请帮助我,谢谢.

I didn't find any solution, may something help me, thank you.

推荐答案

如果我理解正确,您想将您的列表转换为地图.如果是这样,那么你可以这样做:

If I understand correctly, you want to convert your list into map. If so, then you can do this as follows:

locals {
  keycloak_secret_map  = {for idx, val in local.keycloak_secret: idx => val}  
}

产生:

{
  "0" = "account-console"
  "1" = "admin-cli"
  "2" = "broker"
  "3" = "internal"
  "4" = "realm-management"
  "5" = "security-admin-console"
}

这篇关于在 Terraform 中将列表转换为带有索引的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:21