Packer 不会在配置步骤中正确使用私钥进行 SSH 身份验证

sir*_*ide 5 ssh private-key ansible packer

我使用 Packer 构建 VirtualBox 镜像,使用 Ansible 配置器来设置镜像。构建器步骤创建一个临时用户(ssh_usernamessh_password)。Ansible 供应器使用此临时用户运行。当然,我想在设置我们更安全的仅公钥用户后摆脱这个用户。因此,我添加了第二个 Ansible 配置步骤,以安全用户身份连接并删除不安全用户。或者至少这是计划。但是,Ansible via packer 无法使用这种方法实际连接到 VM。

这是 packer.json 文件的相关部分:

"provisioners": [
    {
        "type": "ansible",
        "playbook_file": "playbooks/image/image.yml",
        "groups": [
            "{{user `ansible_group`}}"
        ],
        "user": "vagrant",
        "extra_arguments": [
            "--vault-password-file", "scripts/get-vault-password.sh",
            "-e", "global_configuration_user={{user `configuration_user`}}",
            "-e", "global_deployment_user={{user `deployment_user`}}",
            "-e", "ansible_ssh_pass=vagrant",
            "-vvvvv"
        ]
    },
    {
        "type": "ansible",
        "playbook_file": "playbooks/image/removeVagrant.yml",
        "groups": [
            "{{user `ansible_group`}}"
        ],
        "user": "{{user `configuration_user`}}",
        "extra_arguments": [
            "--vault-password-file", "scripts/get-vault-password.sh",
            "-e", "global_configuration_user={{user `configuration_user`}}",
            "-e", "global_deployment_user={{user `deployment_user`}}",
            "-e", "ansible_ssh_private_key_file=~/.ssh/id_{{user `configuration_user`}}_rsa",
            "-vvvvv"
        ]
    }
],
Run Code Online (Sandbox Code Playgroud)

第一个配置步骤可以正常工作。这是第二个因权限被拒绝而失败。Ansible 正在尝试执行以下 SSH 命令:

ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=37947 -o 'IdentityFile="/home/redacted/.ssh/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=redacted -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ControlPath=/home/redacted/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1491233126.24-276699777493633 `" && echo ansible-tmp-1491233126.24-276699777493633="` echo ~/.ansible/tmp/ansible-tmp-1491233126.24-276699777493633 `" ) && sleep 0'"'"''
Run Code Online (Sandbox Code Playgroud)

SSH 调试输出的相关部分是:

debug1: SSH2_MSG_NEWKEYS received
debug2: key: /home/redacted/.ssh/id_rsa, explicit, agent
debug2: key: redacted
debug2: key: redacted
debug2: key: redacted
debug2: key: redacted
debug2: key: redacted
debug2: key: redacted
debug2: key: redacted
debug3: send packet: type 5
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-with-mic,gssapi-keyex,hostbased,publickey
debug3: authmethod_lookup publickey
debug3: remaining preferred: ,gssapi-keyex,hostbased,publickey
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/redacted/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: key2
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
Run Code Online (Sandbox Code Playgroud)

然后它继续尝试我的.ssh目录中剩余的密钥,当然,这些都失败了。第一个,即请求的那个,已经失败了。

我用 Packer 运行,-on-error=abort这样它就可以让 VM 保持运行状态。我尝试使用相同的命令通过 SSH 连接,但出现连接拒绝错误。我发现,这样做的原因是它试图连接到 Packer 设置的 SSH 代理端口。所以我使用在 Virtualbox VM 上设置的实际转发端口,并且 SSH 连接成功。因此,问题似乎出在 SSH 代理上。但是,我不确定如何使其行为。

我还尝试在 Ansible 配置器的 Packer 配置中使用该ssh_authorized_key_file选项 ( https://www.packer.io/docs/provisioners/ansible.html )。在这种情况下,我收到一个 Packer 错误,说它无法解析授权密钥(源代码在这里:https : //github.com/bhcleek/packer-provisioner-ansible/blob/master/provisioner/ansible/provisioner。去)。它没有告诉我问题是什么。SSH 库的 Go 文档说它是标准的 SSH 密钥文件格式。或者这是我对它的最佳解释(https://godoc.org/golang.org/x/crypto/ssh#ParseAuthorizedKey)。

小智 2

我遇到了这个问题,无法让 Packer SSH 代理成功运行。

为了使打包程序不创建临时密钥,您需要将“配置密钥”烘焙到 AMI 中或提前将其存在于 AWS 上。

如果您遵循选项 1 - 您需要向构建器配置提供两个ssh_private_key_file选项,并设置ssh_agent_auth为 true - 如下所示:

   "ssh_username": "ubuntu",
   "ssh_private_key_file": "../provision",
   "ssh_agent_auth": true,
Run Code Online (Sandbox Code Playgroud)

如果您遵循选项 2 - 将该ssh_keypair_name选项提供给构建者。

在这两种情况下,您都需要向 Ansible 配置程序提供用户,但应使用指定的密钥对,而不是加壳程序生成的临时密钥对。

注意:当我使用 Ansible 配置程序从框中删除用户时,导致 ansible 失败。我怀疑这是因为不通过代理就无法进行 ansible 连接到目标计算机,并且无法在配置程序中指定代理用户。我需要在单个异步调用中执行“删除用户并关闭机器”。