Michael
Michael

Reputation: 2942

"Bad exit status" but no error message - what's failing?

I want to build a custom AMI using packer. Ansible provisioner succeeds but after then there is a bad status code, see log.

There is no AMI generated. I'm wondering what's failing here?

Log

...
    build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: PLAY RECAP *********************************************************************
    build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: imagebase                  : ok=147  changed=116  unreachable=0    failed=0    skipped=4    rescued=0    ignored=1
    build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu:
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Stopping the source instance...
    build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Stopping instance
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Waiting for the instance to stop...
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Waiting for Snapshots to become ready...
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Setting User/Group Permissions for Snapshots...
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Terminating the source AWS instance...
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Bad exit status: -1
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Deleting temporary keypair...
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: Created Volumes: EBS Volumes were created:
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: 
==> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: eu-west-1:vol-044d9b3e8aee028e2
Build 'build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu' finished after 9 minutes 59 seconds.

==> Wait completed after 9 minutes 59 seconds

==> Builds finished. The artifacts of successful builds are:
--> build-agent-base-2023-05-03-1301.amazon-ebsvolume.ubuntu: EBS Volumes were created:

eu-west-1:vol-044d9b3e8aee028e2

Template

packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source  = "github.com/hashicorp/amazon"
    }
    ansible = {
      version = ">= 1.0.2"
      source  = "github.com/hashicorp/ansible"
    }
  }
}

locals {
  packerstarttime = formatdate("YYYY-MM-DD-hhmm", timestamp())
}

source "amazon-ebsvolume" "ubuntu" {

  instance_type = "t2.medium"
  region        = "eu-west-1"

  vpc_id = "vpc-0d47e80bdf0186fd7"
  subnet_id = "subnet-050a7d298317189e0"
  security_group_id = "sg-0d5d0fe6f4cfd1e11"

  source_ami_filter {
    filters = {
      name                = "*_ImgFactory_ubuntu2004-linux-from-ami-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["XXXXXXX"] # hidden
  }

  # Use 12 GB volume (defaults to 8)
  ebs_volumes {
      tags = {
        Name = "build-agent-base"
      }
      volume_type = "gp2"
      device_name = "/dev/sda1"
      delete_on_termination = false
      volume_size = 12
  }

  # Create SSH tunnel via AWS SSM
  ssh_username         = "ubuntu"
  ssh_interface        = "session_manager"
  communicator         = "ssh"
  iam_instance_profile = "remote-access"
}

build {
  name    = "build-agent-base-${local.packerstarttime}"
  sources = ["source.amazon-ebsvolume.ubuntu"]

  provisioner "ansible" {
    playbook_file           = "../../setup-bamboo-agents.yml"
    user                    = "ubuntu"
    inventory_file_template = "imagebase ansible_host={{ .Host }} ansible_user={{ .User }} ansible_port={{ .Port }}\n"
    extra_arguments         = ["--extra-vars", "hosts_pattern=imagebase"]
    ansible_env_vars        = ["ANSIBLE_CONFIG=../../ansible.cfg"]
    timeout                 = "30m"
  }
}

Upvotes: 0

Views: 355

Answers (1)

Michael
Michael

Reputation: 2942

This was a bug in Packer Amazon plugin:

Fixed it by updating to latest version (1.2.5):

packer {
  required_plugins {
    amazon = {
      version = "~> 1.2.5"
      source  = "github.com/hashicorp/amazon"
    }
    ansible = {
      version = "~> 1.0.4"
      source  = "github.com/hashicorp/ansible"
    }
  }
}
packer init -upgrade aws-build-agent.pkr.hcl 

Upvotes: 1

Related Questions