Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass a list as a cli arg: unexpected token while parsing list: IDENT #19401

Closed
arcotek-ltd opened this issue Nov 19, 2018 · 6 comments
Closed
Labels
bug cli powershell Issues that seem to be specific to launching Terraform from PowerShell

Comments

@arcotek-ltd
Copy link

arcotek-ltd commented Nov 19, 2018

Terraform Version

Terraform v0.11.10

Terraform Configuration Files

variable "myVariable" {
    type = "list"
}
output "testOutput" {
    #value = ["${var.myVariable}"]
    value = "${var.myVariable}"
}

Debug Output

2018/11/19 15:00:06 [INFO] Terraform version: 0.11.10  17850e9a55d33c43d7c31fd6ac122ba97a51d899
2018/11/19 15:00:06 [INFO] Go runtime version: go1.11.1
2018/11/19 15:00:06 [INFO] CLI args: []string{"C:\\Terraform\\terraform.exe", "apply", "-var", "'myVariable=[value1]'"}
2018/11/19 15:00:06 [DEBUG] Attempting to open CLI config file: C:\Users\john\AppData\Roaming\terraform.rc
2018/11/19 15:00:06 Loading CLI configuration from C:\Users\john\AppData\Roaming\terraform.rc
2018/11/19 15:00:06 [INFO] CLI command args: []string{"apply", "-var", "'myVariable=[value1]'"}
2018/11/19 15:00:06 [DEBUG] plugin: waiting for all plugin processes to complete...

Expected Behavior

I'd expect a testOutput of "value1".

Actual Behavior

invalid value "myVariable=[value1]" for flag -var: Cannot parse value for variable ("[value1]") as valid HCL: At 1:6: unexpected token while parsing list: IDENT
Usage: terraform plan [options] [DIR-OR-PLAN]

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform apply -var 'myVariable=["value1"]'

Additional Context

I am trying this from the DOS (command line) prompt on Windows 10. I have tried multiple variations of quotes and nothing works, including the confirmation that I am doing it correctly, given by @jbardin in #17032.

References

@apparentlymart
Copy link
Contributor

Hi @arcotek-ltd! Sorry this isn't working properly.

Based on the output from Terraform it looks like the quotes around value1 are being "lost" during command line parsing. The argument parsing rules on Windows are a little different than on Unix systems and so unfortunately more complex examples like this can require some different escaping.

After re-acquainting myself with the command line parsing rules for Windows systems I can see that single quotes are not supported as part of this scheme and that double quotes must be escaped using backslashes. I don't have a Windows system handy to test this right now but based on the rules documented there I would expect the following to work:

terraform apply -var myVariable=[\"value1\"]

The difference from #17032 is that in that issue we were discussing running Terraform from PowerShell, and PowerShell imposes its own parameter parsing rules first, then sticks the parameters it parsed back together so that it can be parsed per the rules above. Because of this, when using PowerShell it's necessary to appease both PowerShell's parser and the normal command line parser, requiring additional characters as discussed there.

To avoid these complexities and cross-platform differences we usually recommend passing non-trivial (i.e. list and map) variables via -var-file instead, since then Terraform is entirely in control of the parsing of the .tfvars file and so it can be interpreted in the same way regardless of operating system.

@apparentlymart apparentlymart added the waiting-response An issue/pull request is waiting for a response from the community label Nov 19, 2018
@arcotek-ltd
Copy link
Author

Thanks @apparentlymart. We actually want to use PowerShell, but couldn't get it to work, so we tried to simplify things by using "DOS".

I'll try your suggestion and update here.

Thanks.

@ghost ghost removed the waiting-response An issue/pull request is waiting for a response from the community label Nov 19, 2018
@apparentlymart apparentlymart added the waiting-response An issue/pull request is waiting for a response from the community label Nov 20, 2018
@arcotek-ltd
Copy link
Author

Mmm, not a nice experience. @apparentlymart mentioned in #17032 that they use DOS to test. May I suggest that when testing Windows, you concentrate on PowerShell over DOS.

Anyway, @apparentlymart's suggestion worked in DOS, and I was able to get it to work by using backslash \.

PowerShell has been a little harder. Referencing the answer here, I've been able to cobble together a mess:

$env:Var1 = "MyVal1"
$env:Var2 = "MyVal2"

terraform apply --% -var "myVariable=[\"%var1%\",\"%var2%\"]" 

I prefer to run CLI stuff through Start-Process, but I couldn't get it to work. Here's what I tried:

$a = "$([char]34)Var1$([char]34)"

$Arguments = @(
"apply" 
#"-var `"myVariable=[$([char]34)Var1$([char]34)]`""
"-var `"myVariable=[$a]`""
)

Start-Process -FilePath terraform.exe -ArgumentList $Arguments -NoNewWindow -Wait -Verbose -PassThru

The above example was one of a number of attempts, replacing backtick (PS escape character) and double-quote with the ASCII code. I also tried using back-slash and escaping that.

Thanks

@ghost ghost removed the waiting-response An issue/pull request is waiting for a response from the community label Nov 20, 2018
@apparentlymart
Copy link
Contributor

Thanks for confirming that the escaping worked in the standard Windows command interpreter, @arcotek-ltd.

Since we already have #17032 open for problems with powershell usage I'd like to consolidate the discussion back over there rather than having two parallel issues. Honestly the issues there are coming from how PowerShell interacts with traditional CLI processes (as opposed to its own applets) rather than anything Terraform itself can really solve -- by the time Terraform is running, PowerShell has already parsed and re-formatted all of the CLI arguments -- and so as I mentioned in the other issue I think the only strong path forward here would be to have a special Terraform PowerShell cmdlet that can work with the PowerShell API directly to extract the arguments and pass them to Terraform in a robust way.

Since the usage with the standard command interpreter is now solved, I'm going to close this out and we can continue investigating PowerShell usage over in #17032. Thanks!

@arcotek-ltd
Copy link
Author

@apparentlymart, could you enlighten me as to what the "PowerShell API" is that you speak of please?

Thanks

@apparentlymart
Copy link
Contributor

Hi @arcotek-ltd,

In the discussion in the other issue there was the idea of writing a Cmdlet that could then accept arguments as cmdlet parameters and escape them properly to pass to Terraform.

@hashicorp hashicorp locked as resolved and limited conversation to collaborators Nov 21, 2018
@apparentlymart apparentlymart added the powershell Issues that seem to be specific to launching Terraform from PowerShell label Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug cli powershell Issues that seem to be specific to launching Terraform from PowerShell
Projects
None yet
Development

No branches or pull requests

1 participant