Guy Wood
Guy Wood

Reputation: 307

Passing map variable to Terraform command line

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)

Upvotes: 7

Views: 7522

Answers (2)

Guy Wood
Guy Wood

Reputation: 307

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.

Upvotes: 5

tomarv2
tomarv2

Reputation: 823

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

Upvotes: 4

Related Questions