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

HCL2: Change tags blocks to be setable ( instead of blocks ) #9024

Closed azr closed 4 years ago

azr commented 4 years ago

Currently tags can only be used this way :

    tags {
        tags_key = "value"
    }

But this prevents from setting these tags from variables; so instead I would like users to be able to use something like this:

    tags = {
        tags_key = "value"
    }

The difference seems trivial but the second option allows to do things like this:

tags = "${merge(
    local.common_tags,
    map(
        "${var.awesome-tag-something}.${var.something_something.name}", "awesome-tag-example"
    )
)}"

This is going to be a breaking change. Note that with HCL v1 these stanzas were interchangeable.


Hopefully this should simply be a type change here:

https://github.com/hashicorp/packer/blob/094b837ed2bb0d19214516b5e10192624acc6a37/cmd/mapstructure-to-hcl2/mapstructure-to-hcl2.go#L262-L267

azr commented 4 years ago

Also in the meantime I recommend using the dynamic block stanza to achieve this:

dynamic "tag" {
    for_each = ${merge(
        local.common_tags,
        map(
            "${var.awesome-tag-something}.${var.something_something.name}", "awesome-tag-example"
        )
    )}
    content {
        name                = tag.key
        value               = tag.value
    }
}
zmingxie commented 4 years ago

@azr I followed your suggestion, and looks like I run into another bug...

.SourceAMI returns <no-value> from the example below:

  dynamic "tag" {
    for_each = merge(
      var.common_tags,
      {
        Name       = "${var.ami_name} {{ isotime \"2006-01-02-15-04\" }}",
        source-ami = "{{ .SourceAMI }}"
      }
    )
    content {
      name  = tag.key
      value = tag.value
    }
  }

Packer logs:

==> amazon-ebs: Creating AMI tags
    amazon-ebs: Adding tag: "Name": "CentOS AMI 2020-04-08-18-48"
    amazon-ebs: Adding tag: "source-ami": "<no value>"
azr commented 4 years ago

Hey @zmingxie we recently merged a fix for this ( original issue ) and I just tagged a nightly that should contain it; please tell us if that works for you.

For your second issue I just opened #9062 which will be addressing this.

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.