Packer: HCL2 AMI_NAME

Created on 28 Oct 2020  Â·  6Comments  Â·  Source: hashicorp/packer

When trying to use multiple builders in an HCL2 formatted packer file, I would think that it would make sense to put the ami_name and ami_description properties within each builder and not the source.

If I use a single source such as the AWS-provide Windows 2016 AMI and I want to create a "base" image and a "hardened" image I have to put the name in the source currently. What would need to be done to move the ami_name property to the builder instead so that I can define it for each builder.

Code Example:

source "amazon-ebs" "windows" {
  ami_name = "Windows2016-{{build.ID}}-{{isotime \"2006-01-02\"}}"
  associate_public_ip_address = false
  communicator                = "winrm"
  force_deregister = true
  force_delete_snapshot = true
  instance_type               = "t2.large"
  region = "${var.aws_region}"
  source_ami_filter {
    filters = {
      name                = "Windows_Server-2016-English-Full-Base*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["amazon"]
  }
  tags = {
    build      = "packer"
    build_date = "{{timestamp}}"
    OS_Version = "Windows 2016"
    Base_AMI_Name = "{{ .SourceAMIName }}"
  }
  user_data_file = "scripts/ec2-userdata.ps1"
  vpc_filter {
    filters = {
      "tag:build" = "packer"
    }
  }
  winrm_insecure = true
  winrm_use_ntlm = true
  winrm_use_ssl  = true
  winrm_username = "Administrator"
}
build {
  name = "base"
  sources = ["source.amazon-ebs.windows"]
  provisioner "powershell" {
    inline = [
      "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/InitializeInstance.ps1 -Schedule",
      "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/SysprepInstance.ps1 -NoShutdown"
    ]
  }
}
build {
  name = "stig"
  sources = ["source.amazon-ebs.windows"]
  provisioner "ansible" {
    ansible_env_vars = ["WINRM_PASSWORD=${build.Password}"]
    extra_arguments  = ["-e", "ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore"]
    playbook_file    = "ansible/stig-playbook.yml"
    use_proxy        = false
    user             = "${build.User}"
  }
  provisioner "windows-restart" {
    restart_timeout = "30m"
  provisioner "powershell" {
    inline = [
      "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/InitializeInstance.ps1 -Schedule",
      "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/SysprepInstance.ps1 -NoShutdown"
    ]
  }
}

docs hcl2 question

All 6 comments

Trying to do this in each builder now:

  source "amazon-ebs.windows" {
    ami_name = "Windows2016-BASE-{{isotime \"2006-01-02\"}}"
    ami_description = "Windows Server 2016 BASE Image by Packer"
  }

This should already work, provided you leave the ami_name and ami_description fields blank in the original source that you defined outside the builder. Packer won't override defined fields, but will happily add fields that aren't defined

Yes, the singular source block inside a build block solves this here. Closing this issue as you seem to have solved it for yourself 🙂 .

Don't hesitate if you have more questions.

We should improve the multi builder documentation for HCL.

Kasey

Sent from my iPhone

On Oct 29, 2020, at 4:48 AM, Adrien Delorme notifications@github.com wrote:


Yes, the singular source block inside a build block solves this here. Closing this issue as you seem to have solved it for yourself 🙂 .

Don't hesitate if you have more questions.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

@kclinden I agree ! What would you do to improve it ? Were would you expect this one to be ? ( Do you have some time to contribute as well ? ) Currently we only have this page https://www.packer.io/docs/from-1.5/blocks/build/source documenting it. Starting here: https://github.com/hashicorp/packer/blob/d8b67f852081315735f5960b1ea7bd106858070f/website/pages/docs/from-1.5/blocks/build/source.mdx#L14-L15

I'll reopen this issue to keep track of this 🙂

Yea I referred to that one actually. I thought it was odd to place it in Build > Source vs in the Overview section. Also maybe it would be helpful if there was in the AWS EBS Builder. One of things that I had to troubleshoot was that you couldn't have the property defined in source and in the build block via source.

It might just be that the example here is too simple. Having more intricate examples helps tremendously.

Was this page helpful?
0 / 5 - 0 ratings