【发布时间】: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