使用Packer构建自定义AMI出现Bad exit status无报错,求排查
问题:Packer构建自定义AMI失败,Ansible执行成功但返回Bad exit status: -1
尝试用Packer构建自定义AMI,Ansible provisioner执行成功,但后续出现Bad exit status: -1错误,未生成目标AMI,仅创建了EBS卷。
相关日志
... 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
Packer模板
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" } }
排查与解决方案
核心问题
你使用了amazon-ebsvolume源类型,这个源的作用是生成EBS卷而非AMI,这就是构建结束后只输出EBS卷、未生成AMI的根本原因。Bad exit status: -1是该源在终止实例后的后续流程异常,根源还是源类型与需求不匹配。
修复步骤
- 替换源类型:将
source "amazon-ebsvolume"改为source "amazon-ebs",这是Packer构建AWS AMI的标准源类型。 - 调整卷配置:
- 移除
ebs_volumes块,改用amazon-ebs支持的volume_size参数直接设置根卷大小(比如volume_size = 12)。如果需要自定义卷标签或其他属性,使用ami_block_device_mappings。 - 删除
delete_on_termination = false,构建AMI时,源实例终止时根卷默认会被删除,不需要保留。
- 移除
- 验证权限:确保
remote-access实例配置文件包含创建AMI所需的权限,比如ec2:CreateImage、ec2:CreateSnapshot、ec2:DescribeInstances等。
修改后的模板核心示例
source "amazon-ebs" "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"] } volume_size = 12 # 设置根卷大小为12GB ami_name = "build-agent-base-${local.packerstarttime}" # 指定AMI名称 # SSH相关配置保留不变 ssh_username = "ubuntu" ssh_interface = "session_manager" communicator = "ssh" iam_instance_profile = "remote-access" } build { name = "build-agent-base-${local.packerstarttime}" sources = ["source.amazon-ebs.ubuntu"] # Ansible provisioner配置保留不变 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" } }
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

