英文:
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'm trying to pass an array to my script file in terraform using **template_file**.
My terraform code is:
```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
...
}
and this is my script:
#!/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
But when I try to run terraform plan
I receive the error:
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" {
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<${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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论