我有一个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": []}
ssh-add -K ~/.ssh/id_rsa
,然后它就可以工作了。 - Will