0

I'm trying to run the following lines of shell script:

var_files=$(var_file_selector)
echo ${var_files}
terraform apply ${var_files} deploy/$1

Where var_files resolves to "deploy/vars/vars.tfvars". When I run the script, I get the following error:

invalid value "\"deploy/vars/vars.tfvars\"" for flag -var-file: Error reading "deploy/vars/vars.tfvars": open "deploy/vars/vars.tfvars": no such file or directory

However if I echo out the whole command:

echo terraform apply ${var_files} deploy/$1

I get:

terraform apply -var-file="deploy/vars/vars.tfvars" deploy/cluster

Which I can run manually from the terminal (in the same working directory that I'm running the script from) and it works just fine. What am I not understanding here?

Andy
  • 3,228
  • 8
  • 40
  • 65
  • Do you not want -var-file=deploy/vars/vars.tfvars without the quotes? – Brian Agnew Mar 06 '19 at 11:30
  • What does `var_file_selector` do and is there a reason you're not just using `terraform.tfvars` and/or `*.auto.tfvars` files? – ydaetskcoR Mar 06 '19 at 11:39
  • @BrianAgnew Yes this works without the quotes. Not sure why it works with them when I manually put the command into the terminal though? – Andy Mar 06 '19 at 11:45
  • @ydaetskcoR var_file_selector just loops over *.tfvars in my vars file dir, and just give each one a '--var-file=...' tag. I didn't know you could just have a specifier as the passed var file. – Andy Mar 06 '19 at 11:46
  • Maybe terraform adds the quotes to the value passed as argument, and as you are alredy including them, they are treated as part of the string, this is why they are escaped in the log you provided: invalid value "\"deploy/vars/vars.tfvars\"" .... – alb3rtobr Mar 06 '19 at 13:06
  • `var_file_selector` appears to be adding quotes when it shouldn't (nor does it need to). See https://stackoverflow.com/a/55023462/1126841. There is a big difference between `x='"foo"'; echo $x` and `x=foo; echo "$x"`. – chepner Mar 06 '19 at 13:47
  • The shell parses quotes before expanding variables, so quotes *in* variables don't get parsed properly (by the time they're there, it's too late for them to work right). See [this question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-shell-syntax-in-arguments-passed-via-variables). – Gordon Davisson Mar 06 '19 at 15:51

0 Answers0