'Terraform template_file issue with a bash script
I am using template_file to pass a list from tfvars to my shell script, I do escape "$" in my tpl.sh script but then my script ends-up ignoring the values of my variables, how to go around this?
In below, the script ignores the values of $${i} and $${device_names[i]} because of the two dollar signs
for i in `seq 0 6`; do
block_device="/dev/nvme$${i}n1"
if [ -e $block_device ]; then
mapping_device="$${device_names[i]}"
I did attempt with eval and got an error in terraform
for i in `seq 0 6`; do
block_device="/dev/nvme" eval "${i}n1"
if [ -e $block_device ]; then
mapping_device=eval "${device_names[i]}"
It turns out that the above issue is that I was not calling the template correctly
But now I am trying to call the template in user_data but getting the below error, can someone pls advice?
locals {
cloud_config_config = <<-END
#cloud-config
${jsonencode({
write_files = [
{
path = "/usr/tmp/myscript.sh.tpl"
permissions = "0744"
owner = "root:root"
encoding = "b64"
#content = "${data.template_file.init.*.rendered}"
},
]
})}
END
myvars = ["var1","var2","var3"]
}
# Template_file
data "template_file" "init" {
template = "${file("${path.module}/script.sh.tpl")}"
vars = {
myvariables = join(" ", local.myvars)
}
}
# cloud-init to copy myscript to the instance "/usr/tmp/myscript.sh.tpl"
# I am not really sure how to move my rendered template into my instance
data "cloudinit_config" "userdata" {
gzip = false
base64_encode = false
part {
content_type = "text/cloud-config"
filename = "cloud-config.yaml"
content = local.cloud_config_config
}
depends_on = [ data.template_file.init ]
}
# User Data
user_data = data.cloudinit_config.userdata.rendered
This configuration throws this error and not really sure why
│ Error: Incorrect attribute value type
│
│ on main.tf line 31, in data "cloudinit_config" "userdata":
│ 31: content = data.template_file.init.*.rendered
│ ├────────────────
│ │ data.template_file.init is object with 5 attributes
│
│ Inappropriate value for attribute "content": string required.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
