【问题标题】:Ansible with Github: Permission denied (Publickey)Ansible with Github:权限被拒绝(Publickey)
【发布时间】:2016-02-12 22:04:39
【问题描述】:

我正在尝试使用 Ansible 了解 GitHub ssh 配置(我正在编写 Ansible:Up & Running 这本书)。我遇到了两个问题。

权限被拒绝(公钥) - 当我第一次运行 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

好的,很公平,我看到有几个人遇到过这个问题。所以我跳到附录 A 关于使用 SSH 运行 Git,它说运行 ssh-agent 并添加 id_rsa 公钥:

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 测试引发了另一个“权限被拒绝(公钥)”错误。

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

该盒子是使用 Mac OS 的 Ubuntu Trusty64。如果有人能告诉我文件权限和/或 Github 密钥生成,我将不胜感激。

【问题讨论】:

  • SSH 密钥是否在您要部署到的服务器上?
  • 您可能会在服务器故障上得到更好的响应。不是真正的编程问题。
  • @mipadi 密钥不在来宾(Vagrant 机器)上。我的印象是转发代理会使用我的 ssh-agent 中的密钥。
  • @YetAnotherUserName 我在那里发帖,虽然我多次搜索时没有出现服务器故障,只是几个 SO 问题。
  • 我遇到了完全相同的问题 - 最后我创建了一个对 repo 具有只读访问权限的部署密钥,将其复制到我的主机上并通过 accept_hostkey 使用它来从回购。不理想,但我和你在同一个圈子里转了几个小时。

标签: python git github ssh ansible


【解决方案1】:

我遇到了这个问题,并通过提高 ansible 命令的详细程度来发现它(对调试非常有用)。

不幸的是,ssh 经常抛出错误消息,并不能完全引导你走向正确的方向(也就是权限被拒绝是非常通用的......虽然公平地说,当存在文件权限问题时经常抛出,所以可能不完全如此通用)。无论如何,运行带有详细信息的 ansible test 命令有助于重现问题并验证问题何时解决。

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

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

步骤在这里Github's helpful ssh docs

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

[paramiko_connection]
record_host_keys = False

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

本质上增加了

-o StrictHostKeyChecking=no

到你的 ssh 参数,并且

-o UserKnownHostsFile=/dev/null

也被添加到 ssh 参数中

在这里找到: Ansible issue 9442

同样,这是在 vagrant VM 上进行的,应在实际服务器上更仔细地考虑主机密钥验证。

希望对你有帮助

【讨论】:

    【解决方案2】:

    我怀疑密钥权限问题是因为您将公钥而不是私钥作为“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.
    

    【讨论】:

    • 我在处理另一个项目时回到了这个问题,并根据另一篇与您的类似的文章进行了一些更改。现在正在工作。谢谢!
    【解决方案3】:

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

    问题是 URL 不正确。

    试着改成:

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

    【讨论】:

      猜你喜欢
      • 2016-05-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 2015-06-18
      • 2015-10-29
      • 2022-08-03
      相关资源
      最近更新 更多