终止具有非零退出状态的源后,Packer 失败:2

Packer failing after terminating source with non-zero exit status: 2

我正在尝试使用 packer 和 ansible 创建一个新的 ami。诚然,我对两者都很陌生。 (我过去使用过 fabric 和 puppet。)我注意到我使用的是 amazon linux base ami,因此决定改用 centos 映像。这带来了一些我已经克服的障碍。但是,我现在收到以下错误。它发生在 "Terminating the source AWS instance..."

之后似乎很奇怪
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: No AMIs to cleanup
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Script exited with non-zero exit status: 2

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Script exited with non-zero exit status: 2

==> Builds finished but no artifacts were created.

包装器 json 是:

{
    "variables": {
        "ansible_staging_directory": "/tmp/packer-provisioner-ansible-local",
        "role": "",
        "aws_base_ami": "",
        "aws_base_instance_type": "",
        "aws_region": "",
        "aws_vpc_id": "",
        "aws_subnet": "",
        "aws_ssh_user": "",
        "company_version": "",
        "additional_aws_users": "{{env `AWS_PROD_ID`}}",        
        "aws_access_key": "<redacted>",
        "aws_secret_key": "<redacted>"
    },
    "builders": [{
        "type": "amazon-ebs",
        "region": "{{user `aws_region`}}",
        "vpc_id": "{{user `aws_vpc_id`}}",
        "subnet_id": "{{user `aws_subnet`}}",
        "source_ami": "{{user `aws_base_ami`}}",
        "instance_type": "{{user `aws_base_instance_type`}}",
        "ssh_username": "{{user `aws_ssh_user`}}",
        "ssh_pty": true,
        "associate_public_ip_address": true,
        "ami_users": "{{user `additional_aws_users`}}",
        "ami_name": "company-{{user `role`}}-{{user `company_version`}} {{timestamp}}",
        "tags": {
            "Role": "company-{{user `role`}}",
            "Version": "{{user `company_version`}}",
            "Timestamp": "{{timestamp}}"
        }
    }],
    "provisioners": [
        {
            "type": "shell",
            "inline": "mkdir -p {{user `ansible_staging_directory`}}"
        },
        {
            "type": "file",
            "source": "../roles",
            "destination": "{{user `ansible_staging_directory`}}"
        },
        {
            "type": "file",
            "source": "../files",
            "destination": "{{user `ansible_staging_directory`}}"
        },
        {
            "type": "shell",
            "inline": [
                "sudo yum -y update",
                "sudo update-alternatives --set python /usr/bin/python2.7",
                "sudo yum -y install emacs",
                "sudo yum -y install telnet",
                "sudo yum -y install epel-release",
                "sudo yum -y install python-pip",
                "sudo yum -y install gcc libffi-devel python-devel openssl-devel",              
                "sudo pip install ansible"
            ]
        },
        {
            "type": "ansible-local",
            "playbook_file": "packer-playbook.yml",
            "group_vars": "../group_vars",
            "extra_arguments": [
                "-t {{user `role`}}",
                "--extra-vars 'aws_access_key={{user `aws_access_key`}} aws_secret_key={{user `aws_secret_key`}}'"
            ]
        }
    ]
}

我使用的基础镜像是ami-d440a6e7。

任何指导将不胜感激。我还没有找到退出代码 2 或类似的东西。

更新

我已通过删除以下行确定:

"sudo update-alternatives --set python /usr/bin/python2.7",

从最后一个 shell provisioner 看来,它似乎完成了该步骤并移至 ansible。但是,ansible 失败了,因为它取决于 python2.7

好的,看来通过删除

"sudo update-alternatives --set python /usr/bin/python2.7",

我能够克服上述错误并发现由于我的 ansible playbook 检查的几个 python 依赖项在 centos yum 回购中的名称与它们在亚马逊-linux 回购。例如,python27-setuptools 就是 python-setuptools,python27-Cython 就是 Cython。

感谢大家的帮助和指导!

感谢 post。 这引导我在尝试 运行 一些脚本(vmware 工具安装)时解决几乎类似的问题。

问题是我 运行 在 vmtools 脚本 rum 之前使用我的清理脚本,这意味着一些必需的文件已经丢失。

vmware-iso: tar: /tmp/VMwareTools-*.tar: Cannot open: No such file or directory
vmware-iso: tar: Error is not recoverable: exiting now

==> vmware-iso:正在停止虚拟机... ==> vmware-iso:删除输出目录... 生成 'vmware-iso' 出错:脚本以非零退出状态退出:2

这是我的配置脚本:

provisioners": [
  "execute_command": "echo 'rhel' | {{.Vars}} sudo -S -E sh -eux '{{.Path}}'",
  "scripts": [
    "scripts/common/metadata.sh",
    "scripts/centos/networking.sh",
    "scripts/common/sshd.sh",
    "scripts/rhel-ec/rhel-user.sh",
    "scripts/rhel-ec/vmtools.sh",
    "scripts/rhel-ec/cleanup.sh",
    "scripts/centos/cleanup.sh",
    "scripts/common/minimize.sh"
  ],
  "type": "shell"
}

],

解决办法是安排我的脚本。这意味着 运行 在 cleanup.sh 运行.

之前先执行 vmtools.sh 脚本