英文:
Dynamic file content in Terraform
问题
I created a file dynamically in terraform. The creation goes into these steps:
- reading the content of some template files
- replacing the variables of the template with variables in terraform
- concatenating all the contents in one single file
The template files are read not following the order I set in the reference terraform map
.. How can I keep the map order in the for loops?
Here the code:
locals {
template_filenames_with_vars = {
# key is template filename, value includes variables to replace in the template file
bbb = {
var = 1
}
aaa = {
var = 1
}
}
}
resource "local_file" "myfile" {
filename = "file.txt"
content = <<EOT
${
join("\n\n", [
for filename, vars in local.template_filenames_with_vars :
templatefile("${filename}.txt", vars)
])
}
EOT
}
In the example, the for loop in the local_file
content loops in the order aaa
,bbb
instead of bbb
,aaa
.
英文:
I created a file dynamically in terraform. The creation goes into these steps:
- reading the content of some template files
- replacing the variables of the template with variables in terraform
- concatenating all the contents in one single file
The template files are read not following the order I set in the reference terraform map
.. How can I keep the map order in the for loops?
Here the code:
locals {
template_filenames_with_vars = {
# key is template filename, value includes variables to replace in the template file
bbb = {
var = 1
}
aaa = {
var = 1
}
}
}
resource "local_file" "myfile" {
filename = "file.txt"
content = <<EOT
${
join("\n\n", [
for filename, vars in local.template_filenames_with_vars :
templatefile("${filename}.txt", vars)
])
}
EOT
}
In the example the for loop in the local_file
content loops in the order aaa
,bbb
instead of bbb
,aaa
.
答案1
得分: 1
Terraform 在 for 循环中使用词汇排序,请参阅元素排序,来自文档:
对于字符串集合,Terraform 根据其值使用词汇排序元素。
所以在这种情况下无法保持映射顺序。
英文:
Terraform uses lexical sorting in for loops, see Element Ordering, from the docs:
> For sets of strings, Terraform sorts the elements by their value, using lexical sorting.
So it's not possible to keep the map order in this case.
答案2
得分: 0
如前面的帖子和评论所说,使用地图无法实现这一点,但如果我使用对象列表,就可以保持顺序:
locals {
template_filenames_with_vars = [
{
name = "bbb"
vars = {
var = 1
}
},
{
name = "aaa"
vars = {
var = 1
}
},
]
}
resource "local_file" "myfile" {
filename = "file.txt";
content = <<EOT
${
join("\n\n", [
for filename in local.template_filenames_with_vars :
templatefile("${filename.name}.txt", filename.vars)
])
}
EOT
}
这将保持列表的顺序(先bbb
,然后aaa
)。
英文:
As previous post and comment said it is not possible to do that with maps, but it is possible to keep order if I use a list of objects:
locals {
template_filenames_with_vars = [
{
name = "bbb"
vars = {
var = 1
}
},
{
name = "aaa"
vars = {
var = 1
}
},
]
}
resource "local_file" "myfile" {
filename = "file.txt"
content = <<EOT
${
join("\n\n", [
for filename in local.template_filenames_with_vars :
templatefile("${filename.name}.txt", filename.vars)
])
}
EOT
}
This keeps the order of the list (bbb
then aaa
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论