I have a bash script which asks for user input to enter 3 master and 5 worker node IPs for a k8s project.
deploy.sh
read -p "Enter first master IP: " MIP_ONE
read -p "Enter second master IP: " MIP_TWO
read -p "Enter third master IP: " MIP_THREE
...
MASTER_IPS_ARRAY="{\"0\"=\"$MIP_ONE\",
\"1\"=\"$MIP_TWO\",
\"2\"=\"$MIP_THREE\"}"
WORKER_IPS_ARRAY="{\"0\"=\"$WIP_ONE\",
\"1\"=\"$WIP_TWO\",
\"2\"=\"$WIP_THREE\",
\"3\"=\"$WIP_FOUR\",
\"4\"=\"$WIP_FIVE\",}"
I’m trying to do something like this in my bash script to update variables in the variables.tf file after it’s been generated.
export TF_VAR_master_ips="{\"0\"=\"$MIP_ONE\",\"1\"=\"$MIP_TWO\",\"2\"=\"$MIP_THREE\"}"
export TF_VAR_worker_ips="{\"0\"=\"$WIP_ONE\",\"1\"=\"$WIP_TWO\",\"2\"=\"$WIP_THREE\",\"3\"=\"$WIP_FOUR\",\"4\"=\"$WIP_FIVE\",}"
I run terraform init and then terraform plan but the output is showing
It’s not creating the correct number of Terraform resources based on the number of IPs. It’s just combining the whole string and acting as one resource. Which can be seen in the ipv4_address argument.
Edit: This bash script works if I use Terraform but not if I use Terragrunt.
I copy a terragrunt.hcl to a project directory and then run terragrunt init, etc. But the export TV_VAR in my bash script doesn’t transfer to the variable.tf correctly.
Hi @techmatlock,
When you specify the value of an input variable using one of the methods that can only support strings as input – environment variables and -var
options – Terraform uses the type
argument in the declaration to infer how it should parse the given string.
In your case I would expect it to work if you specify type = map(string)
, or at least Terraform should generate an explicit error about why it cannot parse the given string as a map.
1 Like
That worked!
I ended up just editing the variables.tf with your suggestions seen here:
variable "master_ips" {
type = map(string)
}
variable "worker_ips" {
type = map(string)
}
Now my deploy.sh bash script looks like this:
.... <REDACTED>
read -p "Enter first master IP: " MIP_ONE
read -p "Enter second master IP: " MIP_TWO
read -p "Enter third master IP: " MIP_THREE
MASTER_IPS_ARRAY="{\"0\"=\"$MIP_ONE\",
\"1\"=\"$MIP_TWO\",
\"2\"=\"$MIP_THREE\"}"
.... <REDACTED>
export TF_VAR_master_ips=$MASTER_IPS_ARRAY
export TF_VAR_worker_ips=$WORKER_IPS_ARRAY
I really appreciate your explanation and suggestion. I was banging my head against the wall why Terraform wasn’t interpreting the string array variable correct in my bash script.