7

Need to pass a map into Terraform via the command line (don't want to use var file or tfvars) and am having an issue with Linux dropping the speech marks. So need something that will be interpreted as a map via Terraform console AND bash echo.

terraform console    
{"ip_add_1"="1.1.1.1","ip_add_2"="2.2.2.2"}

This works but bash (used to run plan) would drop the speech marks which Terraform doesn't like:

echo {"ip_add_1"="1.1.1.1","ip_add_2"="2.2.2.2"}

It also drops the comma but that doesn't matter as I'm only using echo for testing and Terraform plan on the bash shell doesn't do that.

I have tried 1000 variations of escaping, single quotes, etc. But can't find a working solution. For info, I am passing this in using an Azure DevOps variable running on an Ubuntu agent:

terraform plan -var ip_list=$(ip_list)

2 Answers 2

5

Although this didn't work in the Terraform console interpreter, it did solve my problem:

{"\"ip_add_1\"=\"1.1.1.1\",\"ip_add_2\"=\"2.2.2.2\""}

Double quoting everything inside the parenthesis then backslash escaping the other double quotes within that.

Sign up to request clarification or add additional context in comments.

Comments

4

Try this, hope this helps:

variable "demo_var" {}

resource "null_resource" "test" {
  triggers = {
    always_run = timestamp()
  }

  provisioner "local-exec" {
    command = "echo ${var.demo_var} > out.txt"
  }
}

output "demo_output" {
  value = null_resource.test.id
}
terraform plan -var=demo_var='{"aws":"/tmp/demo"}'

If reading from an exported variable:

export demo_var='{"aws":"/tmp/demo_new"}'

Use this:

terraform plan -var=demo_var=$demo_var

1 Comment

Thanks but this was a bit more than I wanted to do

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.