Ansible和Git:在Git Clone时出现Permission denied (publickey)错误

11

我有一个playbook,我想从私有仓库(GIT)克隆到服务器。

我设置了ssh转发,当我ssh进入服务器并尝试手动从同一仓库克隆时,它成功地工作。然而,当我使用ansible来克隆仓库到服务器时,它失败了,显示“Permission Denied Public Key”。

这是我的playbook deploy.yml

---

- hosts: webservers
  remote_user: root

  tasks:
      - name: Setup Git repo
        git: repo={{ git_repo }}
             dest={{ app_dir }}
             accept_hostkey=yes

这是我的ansible.cfg文件的样子:
[ssh_args]
ssh_args = -o FowardAgent=yes

我还能执行playbook中的其他任务(如操作系统操作、安装等)。
我尝试过以下方法:
1. 在服务器上的ansible.cfg文件(与playbook在同一目录下)中设置sshAgentForwarding标志:
``` ssh_args = -o ForwardingAgent=yes ```
2. 使用`become: false`来执行git clone命令。
3. 运行以下命令:
``` ansible -i devops/hosts webservers -a "ssh -T git@bitbucket.org" ```
返回:
``` an_ip_address | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true } ```
这是我用于运行playbook的命令:
``` ansible-playbook devops/deploy.yml -i devops/hosts -vvvv ```
这是我收到的错误消息:
fatal: [162.243.243.13]: FAILED! => {"changed": false, "cmd": "/usr/bin/git ls-remote '' -h refs/heads/HEAD", "failed": true, "invocation": {"module_args": {"accept_hostkey": true, "bare": false, "clone":
 true, "depth": null, "dest": "/var/www/aWebsite", "executable": null, "force": false, "key_file": null, "recursive": true, "reference": null, "refspec": null, "remote": "origin", "repo": "git@bitbucket.org:aUser/aRepo.git", "ssh_opts": null, "track_submodules": false, "update": true, "verify_commit": false, "version": "HEAD"}, "module_name": "git"}, "msg": "Permission denied (publickey).\r\nfatal: Could not r$ad from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.", "rc": 128, "stderr": "Permission denied (publickey).\r\nfatal: Could not read from remote r$pository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n", "stdout": "", "stdout_lines": []}

1
有时候当你更新你的 Mac 时,你的密钥会停止工作(至少对我来说是这样)。我不得不再次运行 ssh-add -K ~/.ssh/id_rsa,然后它就可以工作了。 - Will
4个回答

13

通过阅读ansible中ssh转发的文档,我成功找到了解决方法。

问题在于我的ssh密钥没有被转发,因为默认情况下Ansible不会转发你的密钥,即使你已经在~/.ssh/conf中设置了密钥转发(我在修复问题之前更新了我的问题,添加了ansible.cfg)。

解决方法是在[defaults]下的ansible.cfg中添加transport = ssh,并从ansible.cfg所在的位置运行ansible-playbook,确保目标机器的/etc/ssh/sshd_config中存在以下设置:

AllowAgentForwarding yes

我的ansible.cfg现在长这样:

[defaults]
transport = ssh

[ssh_connection]
ssh_args = -o ForwardAgent=yes

我该如何让Ansible-playbook考虑这个文件。例如:ansible-playbook -i“localhost,”-c local GitClone.yaml。我应该在哪里以及如何包含cfg文件? - Dawny33
1
当您运行 ansible-playbook -i hosts <a_playbook>.yml 时,cfg 文件会自动包含在内。您应该将 cfg 文件放在与要运行的 playbook 相同的目录中。另外,请确保在运行 ansible-playbook 之前运行 ssh-add - lv10

5
为了在远程服务器上克隆私有的Github仓库,我需要执行以下步骤:
首先将ssh密钥添加到您的ssh-agent:
eval `ssh-agent -s`
ssh-add ~/.ssh/my-private-key.pem

之后我修改了ansible.cfg配置文件:

[defaults]
transport = ssh
sudo_flags = -HE

[ssh_connection]
ssh_args = -o ForwardAgent=yes

现在,即使作为root用户,您也可以克隆github私有仓库。

通常,我也会将这两个任务添加到我的playbook/roles tasks中:

- name: Tell the host about our servers it might want to ssh to
  known_hosts:
    path: '/etc/ssh/known_hosts'
    name: 'github.com'
    key: "{{ lookup('pipe', 'ssh-keyscan -t rsa bitbucket.org') }}"

- name: Upload sudo config for key forwarding as root
  lineinfile:
    dest: /etc/sudoers.d/ssh_key_forward
    line: 'Defaults env_keep+=SSH_AUTH_SOCK'
    create: yes
    owner: root 
    group: root 
    mode: "0440"
    state: present
    validate: 'visudo -c -f %s'

奇怪,它对我起作用了。如果ssh选项对您无效,则可以像这样使用用户名/密码选项:

- name: Pull the code
  git:
    repo: "https://{{ bitbucket_login }}:{{ bitbucket_password|urlencode }}@bitbucket.org/path/project.git"
    dest: /var/www/myproject
    version: master

希望这对您和其他人有所帮助。


感谢您抽出时间回复。我尝试了您的建议,但仍然无法解决问题。最终我通过在 ansible.cfg[defaults] 中使用 transport = ssh 来解决了这个问题。 - lv10
@lv10 我已经添加了另一个选项,请检查一下。 - Arbab Nazar
ssh-add 对我有用,谢谢 - Alexander Fadeev

2

对于公共代码库:(您可以使用https)

- name: Git checkout ghq from github
  git:
    repo: https://github.com/x-motemen/ghq.git
    dest: /tmp/ghqt
    depth: "1"

对于私有协议,您可以在之前复制您的私有SSH密钥,并按照以下方式附加:

- name: Git checkout dotfiles repo
  git:
    repo: "https://github.com/x-motemen/ghq.git"
    dest: /tmp/ghqt
    version: "develop"
    accept_hostkey: yes
    key_file: "{{ ssh_key_private_remote_path }}{{ ssh_key_private_filename }}"

更多细节请参考:https://www.jeffgeerling.com/blog/2018/cloning-private-github-repositories-ansible-on-remote-server-through-ssh。这篇文章介绍了如何通过SSH在远程服务器上使用Ansible克隆私有GitHub存储库。

1
在仅限于本地主机的情况下,ForwardAgent 是完全无用的,因为它只会将代理转发到远程主机。
即使手动运行命令行中的 git 可以正常工作,但无论如何都无法从 Ansible 中工作。我找到的唯一有效的解决方案是将 git 转换为 command,例如: - command: /usr/bin/git clone git@github

这不是一个好的解决方案,但它需要更多的任务。Git模块应该可以工作,但我无法通过ssh让它工作。我的Ansible脚本在svn和ssh上都能正常工作。 - BenDavid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接