Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Ansible with Github:权限被拒绝(公钥)_Python_Git_Github_Ssh_Ansible - Fatal编程技术网

Python Ansible with Github:权限被拒绝(公钥)

Python Ansible with Github:权限被拒绝(公钥),python,git,github,ssh,ansible,Python,Git,Github,Ssh,Ansible,我试图理解Ansible的GitHub ssh配置(我正在编写Ansible:Up&Running手册)。我遇到了两个问题 权限被拒绝(公钥)- 当我第一次运行ansible playbook mezzanine.ymlplaybook时,我的权限被拒绝: failed: [web] => {"cmd": "/usr/bin/git ls-remote '' -h refs/heads/HEAD", "failed": true, "rc": 128} stderr: Permission

我试图理解Ansible的GitHub ssh配置(我正在编写Ansible:Up&Running手册)。我遇到了两个问题

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

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代理并添加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测试抛出了另一个“权限被拒绝(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
,但仍然得到
权限拒绝(公钥)
。我甚至试图将密钥输入我的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密钥的生成,我将不胜感激

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

问题是URL不正确

请尝试将其更改为:

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

我怀疑密钥权限问题是因为您将公钥而不是私钥作为段传递给“ssh-I”。请尝试以下方法:

ssh -i ~/.ssh/git_rsa -T git@github.com
(请注意,它是git_rsa而不是git_rsa.pub)

如果这样做有效,那么请确保它位于ssh代理中。加上:

ssh-add ~/.ssh/git_rsa
要验证:

ssh-add -l
然后通过执行以下操作检查Ansible是否尊重代理转发:

ansible web -a "ssh-add -l"
ansible web -a "ssh -T git@github.com"
最后,通过执行以下操作检查您是否可以通过ssh访问GitHub:

ansible web -a "ssh-add -l"
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经常抛出错误消息,这些消息并不能引导您正确的方向(aka permission denied是非常通用的…不过公平地说,当存在文件权限问题时,通常会抛出错误消息,因此可能不太通用)。无论如何,在启用verbose的情况下运行ansible test命令有助于重新创建问题以及验证问题何时得到解决

ansible -vvv all -a "ssh -T git@github.com"
同样,我使用的设置(也是典型的设置)是将ssh密钥加载到控制机器上的代理中并启用转发

步骤可在此处找到

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

[paramiko_connection]
record_host_keys = False
除了控制主机密钥验证的其他配置之外 主机密钥检查=错误

这本质上增加了

-o StrictHostKeyChecking=no
为您发送ssh参数,以及

-o UserKnownHostsFile=/dev/null
也被添加到ssh参数中

可在此处找到:

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


希望这有帮助

您要部署到的服务器上的SSH密钥是否正确?您可能会得到更好的服务器故障响应。这不是一个真正的编程问题。@mipadi钥匙不在客户机上(游民机器)。我的印象是转发代理会使用我的ssh代理的密钥。@YetAnotherUserName我发布到了那里,虽然我搜索了很多次都没有发现服务器故障,只有几个问题。我遇到了完全相同的问题-最后我创建了一个部署密钥,具有对repo的只读访问权限,将其复制到我的主机上,并通过从repo中提取来源。不太理想,但我和你在同一个圈子里转了好几个小时。我在做另一个项目时又回到了这个话题,并根据另一篇文章做了一些修改,这篇文章的修改与你的类似。这是现在的工作。谢谢