hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.04k stars 3.32k forks source link

Build command cannot parse a valid template with the suffix .pkr.json #9112

Closed ChrisArmstrongUK closed 4 years ago

ChrisArmstrongUK commented 4 years ago

Overview of the Issue

The documentation [LINK] suggests that JSON packer templates should have the suffix .pkr.json whilst HCL uses .pkr.hcl. It seems Packer can't parse a template with the suffix .pkr.json. If I remove pkr from the suffix to .json it works as expected.

Reproduction Steps

Create the template and name it template.json Duplicate the template and name it template.pkr.json

Prove both templates are identical diff template.json template.pkr.json

Run packer validate template.json. This will succeed Run packer validate template.pkr.json. This will succeed

Run packer build template.json. This will succeed Run packer build template.pkr.json. Get errors (Error: Extraneous JSON object property)

Packer version

Packer v1.5.5

Simplified Packer Buildfile

{
  "builders": [
    {
      "type": "file",
      "content": "Lorem ipsum dolor sit amet",
      "target": "dummy_artifact"
    }
  ]
}

Operating system and Environment details

macOS 64-bit

Log Fragments and crash.log files

PACKER_LOG=1 packer build template.pkr.json 
2020/04/24 09:20:49 [INFO] Packer version: 1.5.5 [go1.14 darwin amd64]
2020/04/24 09:20:49 Checking 'PACKER_CONFIG' for a config file path
2020/04/24 09:20:49 'PACKER_CONFIG' not set; checking the default config file path
2020/04/24 09:20:49 Attempting to open config file: /Users/chris/.packerconfig
2020/04/24 09:20:49 [WARN] Config file doesn't exist: /Users/chris/.packerconfig
2020/04/24 09:20:49 Setting cache directory: /tmp/packer/packer_cache

  on template.pkr.json line 2:
   2:   "builders": [

No argument or block type is named "builders".

Error: Extraneous JSON object property

  on template.pkr.json line 2:
   2:   "builders": [

No argument or block type is named "builders".

Error: Extraneous JSON object property

  on template.pkr.json line 2:
   2:   "builders": [

No argument or block type is named "builders".

2020/04/24 09:20:49 Build debug mode: false
2020/04/24 09:20:49 Force build: false
2020/04/24 09:20:49 On error: 
2020/04/24 09:20:49 Waiting on builds to complete...
==> Builds finished but no artifacts were created.
azr commented 4 years ago

Howdy @KristophUK, thanks for reporting; recently Packer has incorporated Beta HCL2 support and our HCL2 parsing library can also read JSON. But the schema for JSON - HCL2 and for JSON - JSON are different; when you name your file .pkr.json Packer will try to parse your JSON file using the new schema ( HCL2 - JSON ), you can find HCL2-JSON examples a little down the page you linked, for example here. Now I agree this is totally confusing and will work on the docs a little so that people can see better what's what.

ChrisArmstrongUK commented 4 years ago

Oh of course. Yeah, I got a bit confused with that. Thanks a lot for clearing that up for me.

azr commented 4 years ago

Okay #9113 fixes this 🙂, the docs are going to be update after we release.

ghost commented 4 years ago

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.