如何在 terraform 中正确使用 for_each 中的 each.value? [英] How to correctly use each.value in for_each in terraform?
本文介绍了如何在 terraform 中正确使用 for_each 中的 each.value?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为每个用户创建一个文件,并将分配给他们的项目作为文件的内容.我很高兴得到${each.value}";因为它是一个字符串列表.请问有什么办法吗?
I am trying to create a files for each user with the projects assigned to them as content of the file. I amunable to get the "${each.value}" as it is a list of strings. Any way around this please?
locals {
data = {
"project1" = {
user_assigned = ["user1", "user2", "user3"]
}
"project2" = {
user_assigned = ["user2", "user3", "user4"]
}
}
`
resource "local_file" "foo" {
for_each = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
content = "${each.value}"
filename = "${each.key}"
}
错误:
on test.tf line 85, in resource "local_file" "foo":
85: content = "${each.value}"
|----------------
| each.value is list of string with 2 elements
推荐答案
content
必须是字符串,但在你的情况下它是一个列表,例如[project1"、project2"]
.将其转换为字符串的一种方法是通过 jsonencode:
The content
must be string, but in your case it is a list, e.g. ["project1", "project2"]
. One way to convert it to string is through jsonencode:
resource "local_file" "foo" {
for_each = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
content = jsonencode(each.value)
filename = each.key
}
这篇关于如何在 terraform 中正确使用 for_each 中的 each.value?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文