使用 Github 的 Ansible:权限被拒绝(公钥)

Ansible with Github: Permission denied (Publickey)

我正在尝试使用 Ansible 了解 GitHub ssh 配置(我正在研究 Ansible:Up & 运行 一书)。我 运行 正在处理两个问题。

权限被拒绝(public密钥) - 当我第一次 运行 ansible-playbook mezzanine.yml 剧本时,我的权限被拒绝:

failed: [web] => {"cmd": "/usr/bin/git ls-remote '' -h refs/heads/HEAD", "failed": true, "rc": 128}
stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

msg: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

FATAL: all hosts have already failed -- aborting

好吧,很公平,我看到有几个人遇到过这个问题。所以我在 运行ning Git 上使用 SSH 跳转到附录 A,它说 运行 ssh-agent 并添加 id_rsa public 键:

eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa

输出:Identity Added我运行ssh-agent -l检查并得到长字符串:2048 e3:fb:...但我得到了相同的输出。因此,我查看了有关 ssh 密钥生成和故障排除的 Github 文档,其中建议更新主机上的 ssh 配置文件:

Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile ~/.ssh/id_rsa
    TCPKeepAlive yes
    IdentitiesOnly yes

但这仍然提供相同的错误。所以在这一点上,我开始认为这是我的 rsa 文件,这导致了我的第二个问题。

密钥生成问题 - 我试图生成一个额外的证书来使用,因为 Github 测试抛出了另一个 "Permission denied (publickey)" 错误。

Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
Permission denied (publickey).

我从头开始按照 Github 说明生成了一个具有不同名称的新密钥。

ssh-keygen -t rsa -b 4096 -C "me@example.com"

我没有输入密码并将其保存到名称为 git_rsa.pub 的 .ssh 文件夹中。我 运行 同样的测试并得到以下结果:

$ ssh -i ~/.ssh/git_rsa.pub -T git@github.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/antonioalaniz1/.ssh/git_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: ~/.ssh/github_rsa.pub
Permission denied (publickey).

我检查了权限并对文件执行了 chmod 700,我仍然得到 Permission denied (publickey)。我什至试图将密钥输入我的 Github 帐户,但首先收到一条消息,指出密钥文件需要以 ssh-rsa 开头。所以我开始研究和黑客攻击。开始只是在文件中输入长字符串(以--BEGIN PRIVATE KEY--开头,但失败后我省​​略了那部分);然而,Github 不接受它,说它是无效的。

这是我在 YAML 文件中的 Ansible 命令:

- name: check out the repository on the host
  git: repo={{ repo_url }} dest={{ proj_path }} accept_hostkey=yes

  vars:
    repo_url: git@github.com:lorin/mezzanine-example.git

这是我配置了 ForwardAgent 的 ansible.cfg 文件:

[defaults]
hostfile = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o ForwardAgent=yes

盒子是 UbuntuTrusty64,使用 MacOS。如果有人能给我提供文件权限 and/or Git 集线器密钥生成的线索,我将不胜感激。

我遇到了同样的问题,花了我一些时间,但我找到了解决办法。

问题是 URL 不正确。

只需尝试将其更改为:

repo_url: git://github.com/lorin/mezzanine-example.git

我怀疑密钥权限问题是因为您将 public 密钥而不是私钥作为参数传递给 "ssh -i"。试试这个:

ssh -i ~/.ssh/git_rsa -T git@github.com

(注意是 git_rsa 而不是 git_rsa.pub)。

如果可行,请确保它在您的 ssh-agent 中。添加:

ssh-add ~/.ssh/git_rsa

验证:

ssh-add -l

然后检查 Ansible 是否尊重代理转发:

ansible web -a "ssh-add -l"

最后,检查您是否可以通过 ssh 访问 GitHub:

ansible web -a "ssh -T git@github.com"

您应该会看到如下内容:

web | FAILED | rc=1 >>
Hi lorin! You've successfully authenticated, but GitHub does not provide shell access.

我 运行 进入这个问题并通过提高 ansible 命令的冗长程度发现它(对调试非常有用)。

不幸的是,ssh 经常会抛出错误消息,这些消息并不能完全引导您朝着正确的方向前进(又名权限被拒绝是非常通用的......尽管公平地说,当存在文件权限问题时经常抛出,所以也许不太通用)。无论如何,运行 带有 verbose on 的 ansible test 命令有助于重现问题并验证问题何时解决。

ansible -vvv all -a "ssh -T git@github.com"

同样,我使用的设置(也是一个典型的设置)是将您的 ssh 密钥加载到控制机器上的代理中并启用转发。

步骤在此处 Github's helpful ssh docs

我还注意到,当我通过 vag运行t 命令和 运行 测试连接到盒子本身时,它成功了。所以我把范围缩小到 ansible 是如何转发连接的。对我来说最终起作用的是设置

[paramiko_connection]
record_host_keys = False

除了控制主机密钥验证的其他配置 host_key_checking = 假

这实际上增加了

-o StrictHostKeyChecking=no

给你的 ssh 参数,

-o UserKnownHostsFile=/dev/null

也被添加到 ssh 参数中

在此处找到: Ansible issue 9442

同样,这是在 vag运行t 虚拟机上进行的,在实际服务器上应该更仔细地考虑主机密钥验证。

希望对您有所帮助