Dynamic file content in Terraform

huangapple go评论88阅读模式
英文:

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:

  1. locals {
  2. template_filenames_with_vars = {
  3. # key is template filename, value includes variables to replace in the template file
  4. bbb = {
  5. var = 1
  6. }
  7. aaa = {
  8. var = 1
  9. }
  10. }
  11. }
  12. resource "local_file" "myfile" {
  13. filename = "file.txt"
  14. content = <<EOT
  15. ${
  16. join("\n\n", [
  17. for filename, vars in local.template_filenames_with_vars :
  18. templatefile("${filename}.txt", vars)
  19. ])
  20. }
  21. EOT
  22. }

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:

  1. locals {
  2. template_filenames_with_vars = {
  3. # key is template filename, value includes variables to replace in the template file
  4. bbb = {
  5. var = 1
  6. }
  7. aaa = {
  8. var = 1
  9. }
  10. }
  11. }
  12. resource &quot;local_file&quot; &quot;myfile&quot; {
  13. filename = &quot;file.txt&quot;
  14. content = &lt;&lt;EOT
  15. ${
  16. join(&quot;\n\n&quot;, [
  17. for filename, vars in local.template_filenames_with_vars :
  18. templatefile(&quot;${filename}.txt&quot;, vars)
  19. ])
  20. }
  21. EOT
  22. }

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

如前面的帖子和评论所说,使用地图无法实现这一点,但如果我使用对象列表,就可以保持顺序:

  1. locals {
  2. template_filenames_with_vars = [
  3. {
  4. name = "bbb"
  5. vars = {
  6. var = 1
  7. }
  8. },
  9. {
  10. name = "aaa"
  11. vars = {
  12. var = 1
  13. }
  14. },
  15. ]
  16. }
  17. resource "local_file" "myfile" {
  18. filename = "file.txt";
  19. content = <<EOT
  20. ${
  21. join("\n\n", [
  22. for filename in local.template_filenames_with_vars :
  23. templatefile("${filename.name}.txt", filename.vars)
  24. ])
  25. }
  26. EOT
  27. }

这将保持列表的顺序(先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:

  1. locals {
  2. template_filenames_with_vars = [
  3. {
  4. name = &quot;bbb&quot;
  5. vars = {
  6. var = 1
  7. }
  8. },
  9. {
  10. name = &quot;aaa&quot;
  11. vars = {
  12. var = 1
  13. }
  14. },
  15. ]
  16. }
  17. resource &quot;local_file&quot; &quot;myfile&quot; {
  18. filename = &quot;file.txt&quot;
  19. content = &lt;&lt;EOT
  20. ${
  21. join(&quot;\n\n&quot;, [
  22. for filename in local.template_filenames_with_vars :
  23. templatefile(&quot;${filename.name}.txt&quot;, filename.vars)
  24. ])
  25. }
  26. EOT
  27. }

This keeps the order of the list (bbb then aaa).

huangapple
  • 本文由 发表于 2023年6月30日 00:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定