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

Packer option -var-file=path interpretated with double backslashe #12726

Closed
spawn451 opened this issue Nov 30, 2023 · 4 comments
Closed

Packer option -var-file=path interpretated with double backslashe #12726

spawn451 opened this issue Nov 30, 2023 · 4 comments

Comments

@spawn451
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

When filing a bug, please include the following headings if possible. Any
example text in this template can be deleted.

Overview of the Issue

I'm trying to run Packer from a Powershell script. I use the -var-file=path option to specify the path of my HCL configuration files.
This does not work, Packer can't read the files because the specified paths are always interpreted with double backslashes.

Reproduction Steps

Sample of my powershell script :

# Run Packer
$CONFIG_PATH = (Get-Item .).FullName
$VSPHERE_FILE = "$CONFIG_PATH\vsphere.pkrvars.hcl"
$VAULT_FILE = "$CONFIG_PATH\vault.pkr.hcl"
$COMMON_FILE = "$CONFIG_PATH\common.pkrvars.hcl"
$SCRIPT_PATH = (Get-Item .).FullName
$INPUT_PATH = "$SCRIPT_PATH\builds\windows\server\2022\"

packer init $INPUT_PATH

packer build -force -only vsphere-iso.windows-server-standard-dexp,vsphere-iso.windows-server-standard-core -var-file="$VSPHERE_FILE" -var-file="$VAULT_FILE" -var-file="$COMMON_FILE" "$INPUT_PATH"

Output :

PS D:\_DevOps\_Packer\templates-vmware_v2> D:\_DevOps\_Packer\templates-vmware_v2\build.ps1
Error: Failed to read file

The configuration file
"D:\\_DevOps\\_Packer\\templates-vmware_v2\\vsphere.pkrvars.hcl" could not be
read.

Error: Failed to read file

The configuration file
"D:\\_DevOps\\_Packer\\templates-vmware_v2\\vault.pkr.hcl" could not be read.

Error: Failed to read file

The configuration file
"D:\\_DevOps\\_Packer\\templates-vmware_v2\\common.pkrvars.hcl" could not be
read.

Packer version

From 1.9.4

Operating system and Environment details

Windows 10 22H2

Log Fragments and crash.log files

log detail. PS D:\_DevOps\_Packer\templates-vmware_v2> D:\_DevOps\_Packer\templates-vmware_v2\build.ps1 packer : 2023/11/30 11:43:17 [INFO] Packer version: 1.9.4 [go1.20.7 windows amd64] Au caractère D:\_DevOps\_Packer\templates-vmware_v2\build.ps1:47 : 1 + packer init $INPUT_PATH + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (2023/11/30 11:4... windows amd64]:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

2023/11/30 11:43:17 [TRACE] discovering plugins in C:\Program Files\HashiCorp
2023/11/30 11:43:17 [TRACE] discovering plugins in .
2023/11/30 11:43:17 [TRACE] discovering plugins in C:\Users\DJE\AppData\Roaming\packer.d\plugins
2023/11/30 11:43:17 [INFO] Discovered potential plugin: ansible = C:\Users\DJE\AppData\Roaming\packer.d\plugins\github.com\hashicorp\ansible\packer-plugin-ansible_v1.1.1_x5.0_windows_amd64.exe
2023/11/30 11:43:17 [INFO] Discovered potential plugin: vsphere = C:\Users\DJE\AppData\Roaming\packer.d\plugins\github.com\hashicorp\vsphere\packer-plugin-vsphere_v1.2.2_x5.0_windows_amd64.exe
2023/11/30 11:43:17 [INFO] Discovered potential plugin: windows-update = C:\Users\DJE\AppData\Roaming\packer.d\plugins\github.com\rgl\windows-update\packer-plugin-windows-update_v0.14.3_x5.0_windows_amd64.exe
2023/11/30 11:43:17 found external [-packer-default-plugin-name- local] provisioner from ansible plugin
2023/11/30 11:43:18 [INFO] found external [clone iso supervisor] builders from vsphere plugin
2023/11/30 11:43:18 [INFO] found external [-packer-default-plugin-name- template] post-processors from vsphere plugin
2023/11/30 11:43:18 found external [-packer-default-plugin-name-] provisioner from windows-update plugin
2023/11/30 11:43:18 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2023/11/30 11:43:18 [INFO] PACKER_CONFIG env var set; attempting to open config file: C:\Users\DJE\AppData\Roaming\packer.config
2023/11/30 11:43:18 [WARN] Config file doesn't exist: C:\Users\DJE\AppData\Roaming\packer.config
2023/11/30 11:43:18 [INFO] Setting cache directory: D:_DevOps_Packer\templates-vmware_v2\packer_cache
e: cannot determine if process is in background: Process background check error: not implemented yet
2023/11/30 11:43:18 [TRACE] init: plugingetter.ListInstallationsOptions{FromFolders:[]string{"C:\Program Files\HashiCorp", ".", "C:\Users\DJE\AppData\Roaming\packer.d\plugins"},
BinaryInstallationOptions:plugingetter.BinaryInstallationOptions{APIVersionMajor:"5", APIVersionMinor:"0", OS:"windows", ARCH:"amd64", Ext:".exe", Checksummers:[]plugingetter.Checksummer{plugingetter.Checksummer{Type:"sha256",
Hash:(*sha256.digest)(0xc0004ea480)}}}}
2023/11/30 11:43:18 [TRACE] listing potential installations for "github.com/hashicorp/vsphere" that match ">= 1.2.1". plugingetter.ListInstallationsOptions{FromFolders:[]string{"C:\Program Files\HashiCorp", ".",
"C:\Users\DJE\AppData\Roaming\packer.d\plugins"}, BinaryInstallationOptions:plugingetter.BinaryInstallationOptions{APIVersionMajor:"5", APIVersionMinor:"0", OS:"windows", ARCH:"amd64", Ext:".exe",
Checksummers:[]plugingetter.Checksummer{plugingetter.Checksummer{Type:"sha256", Hash:(*sha256.digest)(0xc0004ea480)}}}}
2023/11/30 11:43:18 [TRACE] for plugin github.com/hashicorp/vsphere found 1 matching installation(s)
2023/11/30 11:43:18 [TRACE] listing potential installations for "github.com/rgl/windows-update" that match ">= 0.14.3". plugingetter.ListInstallationsOptions{FromFolders:[]string{"C:\Program Files\HashiCorp", ".",
"C:\Users\DJE\AppData\Roaming\packer.d\plugins"}, BinaryInstallationOptions:plugingetter.BinaryInstallationOptions{APIVersionMajor:"5", APIVersionMinor:"0", OS:"windows", ARCH:"amd64", Ext:".exe",
Checksummers:[]plugingetter.Checksummer{plugingetter.Checksummer{Type:"sha256", Hash:(*sha256.digest)(0xc0004ea480)}}}}
2023/11/30 11:43:18 [TRACE] for plugin github.com/rgl/windows-update found 1 matching installation(s)
2023/11/30 11:43:18 [INFO] (telemetry) Finalizing.
2023/11/30 11:43:18 waiting for all plugin processes to complete...
packer : 2023/11/30 11:43:19 [INFO] Packer version: 1.9.4 [go1.20.7 windows amd64]
Au caractère D:_DevOps_Packer\templates-vmware_v2\build.ps1:49 : 1

  • packer build -force -only vsphere-iso.windows-server-standard-dexp,vs ...
  •   + CategoryInfo          : NotSpecified: (2023/11/30 11:4... windows amd64]:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError
    
    

2023/11/30 11:43:19 [TRACE] discovering plugins in C:\Program Files\HashiCorp
2023/11/30 11:43:19 [TRACE] discovering plugins in .
2023/11/30 11:43:19 [TRACE] discovering plugins in C:\Users\DJE\AppData\Roaming\packer.d\plugins
2023/11/30 11:43:19 [INFO] Discovered potential plugin: ansible = C:\Users\DJE\AppData\Roaming\packer.d\plugins\github.com\hashicorp\ansible\packer-plugin-ansible_v1.1.1_x5.0_windows_amd64.exe
2023/11/30 11:43:19 [INFO] Discovered potential plugin: vsphere = C:\Users\DJE\AppData\Roaming\packer.d\plugins\github.com\hashicorp\vsphere\packer-plugin-vsphere_v1.2.2_x5.0_windows_amd64.exe
2023/11/30 11:43:19 [INFO] Discovered potential plugin: windows-update = C:\Users\DJE\AppData\Roaming\packer.d\plugins\github.com\rgl\windows-update\packer-plugin-windows-update_v0.14.3_x5.0_windows_amd64.exe
2023/11/30 11:43:19 [INFO] found external [clone iso supervisor] builders from vsphere plugin
2023/11/30 11:43:19 [INFO] found external [-packer-default-plugin-name- template] post-processors from vsphere plugin
2023/11/30 11:43:19 found external [-packer-default-plugin-name-] provisioner from windows-update plugin
2023/11/30 11:43:19 found external [-packer-default-plugin-name- local] provisioner from ansible plugin
2023/11/30 11:43:19 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2023/11/30 11:43:19 [INFO] PACKER_CONFIG env var set; attempting to open config file: C:\Users\DJE\AppData\Roaming\packer.config
2023/11/30 11:43:19 [WARN] Config file doesn't exist: C:\Users\DJE\AppData\Roaming\packer.config
2023/11/30 11:43:19 [INFO] Setting cache directory: D:_DevOps_Packer\templates-vmware_v2\packer_cache
e: cannot determine if process is in background: Process background check error: not implemented yet
The configuration file
"D:\_DevOps\_Packer\templates-vmware_v2\vsphere.pkrvars.hcl" could not be
read.
Error: Failed to read file
Error: Failed to read file
The configuration file
"D:\_DevOps\_Packer\templates-vmware_v2\vault.pkr.hcl" could not be read.
Error: Failed to read file

The configuration file
"D:\_DevOps\_Packer\templates-vmware_v2\common.pkrvars.hcl" could not be
read.
2023/11/30 11:43:19 [INFO] (telemetry) Finalizing.
The configuration file
"D:\_DevOps\_Packer\templates-vmware_v2\vsphere.pkrvars.hcl" could not be
read.

Error: Failed to read file

The configuration file
"D:\_DevOps\_Packer\templates-vmware_v2\vault.pkr.hcl" could not be read.

Error: Failed to read file

The configuration file
"D:\_DevOps\_Packer\templates-vmware_v2\common.pkrvars.hcl" could not be
read.

2023/11/30 11:43:19 waiting for all plugin processes to complete...

@spawn451 spawn451 added the bug label Nov 30, 2023
@lbajolet-hashicorp
Copy link
Contributor

Hi @spawn451,

The escaped \\ you see are a cosmetic problem more than a bug in Packer in this case. The messages are printed out with %s, which when used in Go's format-strings print out the argument as a quoted and escaped string following Go rules, such that you could import it in a Go file and see the source string verbatim.

The error you're getting is likely to be something else here, unfortunately the hcl library which implements the code producing the diagnostic you see does not include the error in its output, which I'll submit a PR for soon. If accepted we'll get more insight in the problem itself.

Aside from that, I think you may have a usage problem, I presume the first error is thrown by packer init, and the others are thrown by packer build?
Could you share the layout of your file hierarchy so we can figure out if there's something to fix in the command-line itself?

Thanks!

@spawn451
Copy link
Author

Hello thanks! After carefully reading the log, I noticed that the escaped \\ was indeed cosmetic as you said.

for the packer build issue :
I found out that my paths were not correctly defined. The builds folder was missing.

$VSPHERE_FILE = "$CONFIG_PATH\builds\vsphere.pkrvars.hcl"
...

for the packer init issue :
This appen when I execute my Powershell script using Windows PowerShell ISE.
No more packer init error when executing the script with the Windows PowerShell console.

@lbajolet-hashicorp
Copy link
Contributor

Hi @spawn451,

I closed the issue since from the last message you shared this looks like it was fixed, and not a Packer bug.

Feel free to reopen if I'm mistaken!

Copy link

github-actions bot commented Jan 5, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants