Dynamic file content in Terraform

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

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 &quot;local_file&quot; &quot;myfile&quot; {
  filename = &quot;file.txt&quot;

  content = &lt;&lt;EOT
${
  join(&quot;\n\n&quot;, [
    for filename, vars in local.template_filenames_with_vars :
        templatefile(&quot;${filename}.txt&quot;, 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 = &quot;bbb&quot;
      vars = {
        var = 1
      }
    },
    {
      name = &quot;aaa&quot;
      vars = {
        var = 1
      }
    },
  ]
}

resource &quot;local_file&quot; &quot;myfile&quot; {
  filename = &quot;file.txt&quot;

  content = &lt;&lt;EOT
${
  join(&quot;\n\n&quot;, [
    for filename in local.template_filenames_with_vars :
        templatefile(&quot;${filename.name}.txt&quot;, filename.vars)
  ])
}
  EOT
}

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:

确定