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.11k stars 3.33k forks source link

[HCL2] `filters` block is no longer recognized. #9214

Closed orendain closed 4 years ago

orendain commented 4 years ago

Overview of the Issue

Some (HCL) blocks that used to work pre v1.5.6, now do not. I'm specifically referring to filters when used within a source_ami_filter block (that's what I tested), but it's possible that the other blocks listed in the PR below were affected as well.

It is related to PR https://github.com/hashicorp/packer/pull/8889, which was created to address issue https://github.com/hashicorp/packer/issues/8684.

The PR above suggests that the filters { ...} block will continue to be supported. However, that doesn't seem to be the case.

Reproduction Steps

Consider the following HCL:

source "amazon-ebs" "test-ami" {

  // ...

  source_ami_filter {
    filters {
      virtualization-type = "hvm"
      name = "ubuntu/images/*ubuntu-bionic-18.04-amd64-server-*"
      root-device-type = "ebs"
    }
    owners = ["099720109477"]
    most_recent = true
  }

}

Pre v1.5.6, the above works fine. Starting with Packer v1.5.6, the following error occurs:

on sources.pkr.hcl line 22, in source "amazon-ebs" "test-ami":
  22:     filters {

Blocks of type "filters" are not expected here. Did you mean "filter"?

Additionally (note, this may or may not be a separate issue - but they definitely appear related), modifying the code to satisfy the format in the above PR results in the following:


source_ami_filter {
    filter {
      name = "virtualization-type"
      value = "hvm"
    }

    filter {
      name = "name"
      value = "ubuntu/images/*ubuntu-bionic-18.04-amd64-server-*"
    }

    filter {
      name = "root-device-type"
      value = "ebs"
    }

  }

This also throws errors:

Error: Unsupported argument

  on sources.pkr.hcl line 24, in source "amazon-ebs" "test-ami":
  24:       name = "virtualization-type"

An argument named "name" is not expected here.

Packer version

From v1.5.6. The first block of code above worked fine in v1.5.5.

azr commented 4 years ago

Hello there, sorry you had this and thanks for trying HCL2 !!

This is a breaking change we brought in HCL2 to allow setting those fields from variables; So to quickly fix: filters { becomes filters = {. The PR bringing this change is this one: https://github.com/hashicorp/packer/pull/9035 and it's also in the CHANGELOG file.

It is now also possible to set repeatable singular blocks like filter or tag but with singular key/name & value settings. ( those are intended to be used with the dynamic block setting, they will be extended in the future as well ).

Please note that HCL2 is still in beta and some breaking change might occur so please make sure to check the CHANGELOG for each version ! :)

That said I do not expect any more major breaking change coming any soon.

I'm going to close this issue because I think it is solved; do not hesitate to ask more questions though 🙂.

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.