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

Terraform -var-file not working for file present in the same directory #11112

Closed
Snehac opened this issue Jan 9, 2017 · 9 comments
Closed

Terraform -var-file not working for file present in the same directory #11112

Snehac opened this issue Jan 9, 2017 · 9 comments
Labels
bug core powershell Issues that seem to be specific to launching Terraform from PowerShell windows

Comments

@Snehac
Copy link

Snehac commented Jan 9, 2017

I am trying to pass file name as command Line argument and following the examples i found on internet. following is the command i am using :

terraform plan -var-file=cert.tf
and get the following error:
invalid value "cert" for flag -var-file: Error reading cert: open cert: The system cannot find the file specified

Cert.tf is present in the same path where i am executing the command. I am using Windows machine and terraform --version is 0.8.2.

This might not be the right place to ask this .. But i am not getting help from anywhere else

@apparentlymart
Copy link
Contributor

Hi @Snehac! Sorry for the silence here.

Usually this error message reflects the entire filename provided, so it's strange that for you it is showing just "cert" rather than "cert.tf". For example, here's the error message I got when I intentionally ran with a missing cert.tf:

invalid value "cert.tf" for flag -var-file: Error reading cert.tf: open cert.tf: The system cannot find the file specified

So this suggests either that the command line was somehow parsed incorrectly or that there was an extra space or other separator character before the .tf in your command line.

Note also that files passed to -var-file should not normally have a .tf extension because that extension is reserved for the main configuration files, not for the "vars" files. A conventional extension for a vars file is .tfvars.

Can you confirm that this issue is still occurring for you?

@apparentlymart apparentlymart added the waiting-response An issue/pull request is waiting for a response from the community label May 3, 2017
@Snehac
Copy link
Author

Snehac commented May 10, 2017

Hi @apparentlymart . Thanks for getting back to me.

i created a cert.tfvars file and tried apply it . but same results.

scharkha@92CSK32-win10 MINGW64 ~/terraform (feature/CORE-1805)
$ terraform apply -var-file=cert.tfvars
invalid value "cert.tfvars" for flag -var-file: multiple map declarations not supported for variables
Usage: terraform apply [options] [DIR-OR-PLAN]

@apparentlymart
Copy link
Contributor

Hi @Snehac!

This latest message seems to be different than the one you shared before. In this case it looks like Terraform did indeed succeed in opening the file, but it failed to process it. From the name it sounds like it'll have sensitive information in it, but if you could share an example of that file with all of the string values removed (still showing as much of the structure as possible) that would help understand what Terraform is complaining about.

@Snehac
Copy link
Author

Snehac commented May 11, 2017

Hello @apparentlymart ,

So i finally had a conclusive observation. When i am running the command on powershell we hit the first issue:

POWERSHELL OUTPUT:
terraform apply -var-file=cert.tf
terraform : invalid value "cert" for flag -var-file: Error reading cert: open cert: The system cannot find the file specified.
At line:1 char:1

  • terraform apply -var-file=cert.tf

I tried the same on git bash i see this :

$ terraform apply -var-file=cert.tf
invalid value "cert.tf" for flag -var-file: Error reading cert.tf: open cert.tf: The system cannot find the file specified.

@apparentlymart
Copy link
Contributor

Thanks for that, @Snehac. Just to confirm: I see you using cert.tf now but previously you were using cert.tfvars. The latter is the correct filename to use, since the .tf extension is reserved for config files, not vars files. Does it in either case if you use cert.tfvars instead (and, of course, make sure your file actually has that name)

@apparentlymart
Copy link
Contributor

It looks like PowerShell parses command lines in a different way that causes it to, for some reason, truncate the file extension from the filename:

powershell-eats-file-extension

I'm going to look deeper into why that is, but for now the workaround is to type the filename in quotes to help PowerShell's parser understand that it should be treated as a single, literal unit.

@apparentlymart apparentlymart removed the waiting-response An issue/pull request is waiting for a response from the community label May 11, 2017
@apparentlymart
Copy link
Contributor

After some more investigation, I've learned that PowerShell parses the unquoted command line into elements like this:

  • E:\terraform.exe
  • plan
  • -var-file=vars
  • .tfplan

It doesn't seem like there's anything we can do to change how PowerShell interprets this command line. Powershell users must just always enclose arguments in quotes to avoid PowerShell breaking them.

Since this seems to be standard PowerShell behavior and there's nothing we can do about it, I'm going to close this. Thanks for your patience working through this with me, @Snehac!

@waynekhan
Copy link

Hello @apparentlymart,

Putting the value in double quotes worked for me today, thanks!

PS>
terraform --version
Terraform v0.11.10

PS> terraform plan -out terraform.plan -var-files="variables.tfvars"
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.

Onwards!

@ghost
Copy link

ghost commented Mar 30, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
@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 core powershell Issues that seem to be specific to launching Terraform from PowerShell windows
Projects
None yet
Development

No branches or pull requests

4 participants