Terraform template_file 失败渲染

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

Terraform template_file failed to render

问题

I'm here to provide the translation of the code you provided. Here's the translated code:

  1. 我正在尝试使用**template_file**将数组传递给我的Terraform脚本文件
  2. 我的Terraform代码如下
  3. ```hcl
  4. variable "services" {
  5. type = list(string)
  6. default = ["Service1", "Service2", "service3"]
  7. }
  8. variable "passwords" {
  9. type = list(string)
  10. default = ["Password1", "Password2", "Password3"]
  11. }
  12. data "template_file" "configure" {
  13. template = "${file("${path.module}/init/configure")}"
  14. vars = {
  15. services = join(",", var.services)
  16. passwords = join(",", var.passwords)
  17. }
  18. }
  19. resource "aws_instance" "grafana_nodes" {
  20. ...
  21. user_data = data.template_file.config
  22. ...
  23. }

这是我的脚本:

  1. #!/bin/bash
  2. declare -a services_array
  3. services_array=($(echo "${services}" | tr "," " "))
  4. declare -a passwords_array
  5. passwords_array=($(echo "${passwords}" | tr "," " "))
  6. sudo echo "${services_array[@]}" >> /tmp/isp_services_array.txt
  7. sudo echo "${passwords_array[@]}" >> /tmp/influx_db_passwords_array.txt

但是当我尝试运行terraform plan时,我收到以下错误:

  1. Error: failed to render: <template_file>:8,29-30: Invalid character; This character is not used within the language., and 2 other diagnostic(s)
  2. with module.mymodule.data.template_file.configure,
  3. on modules/mymodule/main.tf line 417, in data "template_file" "configure":
  4. 417: data "template_file" "configure" {

有人知道如何在我的脚本中使用数组值吗?
如果我在内部运行脚本(登录到EC2实例),则正常工作。

  1. Please note that the translated code remains the same, and I haven't included additional information. If you have any further questions or need assistance with specific parts of the code, please let me know.
  2. <details>
  3. <summary>英文:</summary>
  4. I&#39;m trying to pass an array to my script file in terraform using **template_file**.
  5. My terraform code is:
  6. ```hcl
  7. variable &quot;services&quot; {
  8. type = list(string)
  9. default = [&quot;Service1&quot;, &quot;Service2&quot;, &quot;service3&quot;]
  10. }
  11. variable &quot;passwords&quot; {
  12. type = list(string)
  13. default = [&quot;Password1&quot;, &quot;Password2&quot;, &quot;Password3&quot;]
  14. }
  15. data &quot;template_file&quot; &quot;configure&quot; {
  16. template = &quot;${file(&quot;${path.module}/init/configure&quot;)}&quot;
  17. vars = {
  18. services = join(&quot;,&quot;,var.services)
  19. passwords = join(&quot;,&quot;,var.passwords)
  20. }
  21. }
  22. resource &quot;aws_instance&quot; &quot;grafana_nodes&quot; {
  23. ...
  24. user_data = data.template_file.config
  25. ...
  26. }

and this is my script:

  1. #!/bin/bash
  2. declare -a services_array
  3. services_array=($(echo &quot;${services}&quot; | tr &quot;,&quot; &quot; &quot;))
  4. declare -a passwords_array
  5. passwords_array=($(echo &quot;${passwords}&quot; | tr &quot;,&quot; &quot; &quot;))
  6. sudo echo &quot;${services_array[@]}&quot; &gt;&gt; /tmp/isp_services_array.txt
  7. sudo echo &quot;${passwords_array[@]}&quot; &gt;&gt; /tmp/influx_db_passwords_array.txt

But when I try to run terraform plan I receive the error:

  1. Error: failed to render : &lt;template_file&gt;:8,29-30: Invalid character; This character is not used within the language., and 2 other diagnostic(s)
  2. with module.mymodule.data.template_file.configure,
  3. on modules/mymodule/main.tf line 417, in data &quot;template_file&quot; &quot;configure&quot;:
  4. 417: data &quot;template_file&quot; &quot;configure&quot; {

Could someone know how can use an array values in my script?
If I run the script internally (loggin to the ec2 instance) works fine.

答案1

得分: 0

感谢大家的支持。

我是这样解决的:

  1. #!/bin/bash
  2. for (( j=1; j<${count_services}+1; j++ ));
  3. do
  4. service=$(sudo echo "${services}" | cut -d ',' -f $j)
  5. password=$(sudo echo "${passwords}" | cut -d ',' -f $j)
  6. sudo echo "service: $service password: $password" >> /tmp/my_output.txt
  7. done
英文:

Thanks all for the support.

I solved in this way:

  1. #!/bin/bash
  2. for (( j=1; j&lt;${count_services}+1; j++ ));
  3. do
  4. service=$(sudo echo &quot;${services}&quot; | cut -d &#39;,&#39; -f $j)
  5. password=$(sudo echo &quot;${passwords}&quot; | cut -d &#39;,&#39; -f $j)
  6. sudo echo &quot;service: $service password: $password&quot; &gt;&gt; /tmp/my_output.txt
  7. done

huangapple
  • 本文由 发表于 2023年7月6日 17:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627292.html
匿名

发表评论

匿名网友

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

确定