0
votes

I am looking to pass variables to a script on a new created aws_instance, but I'm not sure how. Here is what I have:

provisioner "file" {
    source      = "${path.module}/scripts/slave-provisioner.sh"
    destination = "/tmp/slave-provisioner.sh"
}

provisioner "remote-exec" {
    inline = [
        "chmod +x /tmp/slave-provisioner.sh",
        "sudo /tmp/slave-provisioner.sh ${split(",",var.zookeeper_ips_list)}"
    ]
}

I have verified that the zookeeper_ips_list has the correct list of IPs.

There error that I get via terraform apply is:

 /tmp/terraform_939984059.sh: 5: /tmp/terraform_939984059.sh: 10.239.12.181: not found

So basically I want to create this machine and then kick of my provisioner script that gets passed a list of IPs so it can do it's configuration.

2
This looks pretty odd to me as you are taking a comma separated string and turning it into an array which you then pass to your provisioner script. I'm not 100% sure what the expected behaviour actually is for that as I'd expect it to want you to pass it as a string. Are you really meaning to do that? Or would you be better off replacing the commas with spaces (using replace())?ydaetskcoR
We are passing those IP addresses into a bash script that just updates the zookeeper configuration for our cluster. The problem was solved with just some bash formatting.David Ficociello

2 Answers

1
votes

Hope you run terraform with latest version.

Seems terraform gets the value of variable var.zookeeper_ips_list, but the double quotes in double quotes makes the trouble. You may try to escape them by \, I didn't try the code, please let me know the result

"sudo /tmp/slave-provisioner.sh ${split(\",\",var.zookeeper_ips_list)}"

And you can take it out, let me know the result as well.

zookeeper_ips_list = ${split(",",var.zookeeper_ips_list)}

provisioner "remote-exec" {
    inline = [
        "chmod +x /tmp/slave-provisioner.sh",
        "sudo /tmp/slave-provisioner.sh ${zookeeper_ips_list)}"
    ]
1
votes

This was solved via some unrelated bash cleanup. The format for the Terraform is correct and it was operating as expected. We weren't dealing with the incoming IP addresses properly in our bash script and so it was failing.

Terraform isn't the most robust when it comes to debugging so this was a bit tricky to track down.