Terraform template_file 失败渲染

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

Terraform template_file failed to render

问题

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

我正在尝试使用**template_file**将数组传递给我的Terraform脚本文件

我的Terraform代码如下

```hcl
variable "services" {
   type = list(string)
   default = ["Service1", "Service2", "service3"]
}

variable "passwords" {
   type = list(string)
   default = ["Password1", "Password2", "Password3"]
}

data "template_file" "configure" {
  template = "${file("${path.module}/init/configure")}"
  vars = {
    services  = join(",", var.services)
    passwords = join(",", var.passwords)
  }
}

resource "aws_instance" "grafana_nodes" {
  ...
  user_data = data.template_file.config
  ...
}  

这是我的脚本:

#!/bin/bash
declare -a services_array
services_array=($(echo "${services}" | tr "," " "))

declare -a passwords_array
passwords_array=($(echo "${passwords}" | tr "," " "))

sudo echo "${services_array[@]}" >> /tmp/isp_services_array.txt
sudo echo "${passwords_array[@]}" >> /tmp/influx_db_passwords_array.txt

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

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

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


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.

<details>
<summary>英文:</summary>

I&#39;m trying to pass an array to my script file in terraform using **template_file**.

My terraform code is:

```hcl
variable &quot;services&quot; {
   type = list(string)
   default = [&quot;Service1&quot;, &quot;Service2&quot;, &quot;service3&quot;]
}

variable &quot;passwords&quot; {
   type = list(string)
   default = [&quot;Password1&quot;, &quot;Password2&quot;, &quot;Password3&quot;]
}

data &quot;template_file&quot; &quot;configure&quot; {
  template = &quot;${file(&quot;${path.module}/init/configure&quot;)}&quot;
  vars = {
    services  = join(&quot;,&quot;,var.services)
    passwords = join(&quot;,&quot;,var.passwords)

  }
}

resource &quot;aws_instance&quot; &quot;grafana_nodes&quot; {
  ...
  user_data = data.template_file.config
  ...
}  

and this is my script:

#!/bin/bash
declare -a services_array
services_array=($(echo &quot;${services}&quot; | tr &quot;,&quot; &quot; &quot;))

declare -a passwords_array
passwords_array=($(echo &quot;${passwords}&quot; | tr &quot;,&quot; &quot; &quot;))

sudo echo &quot;${services_array[@]}&quot; &gt;&gt; /tmp/isp_services_array.txt
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:

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)
│ 
│   with module.mymodule.data.template_file.configure,
│   on modules/mymodule/main.tf line 417, in data &quot;template_file&quot; &quot;configure&quot;:
│  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

感谢大家的支持。

我是这样解决的:

#!/bin/bash

for (( j=1; j<${count_services}+1; j++ ));
do
  service=$(sudo echo "${services}" | cut -d ',' -f $j)
  password=$(sudo echo "${passwords}" | cut -d ',' -f $j)
  sudo echo "service: $service password: $password" >> /tmp/my_output.txt
done
英文:

Thanks all for the support.

I solved in this way:

#!/bin/bash

for (( j=1; j&lt;${count_services}+1; j++ ));
do
  service=$(sudo echo &quot;${services}&quot; | cut -d &#39;,&#39; -f $j)
  password=$(sudo echo &quot;${passwords}&quot; | cut -d &#39;,&#39; -f $j)
  sudo echo &quot;service: $service password: $password&quot; &gt;&gt; /tmp/my_output.txt
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:

确定