2016-09-22 99 views
3

我有一個劇本,我試圖從私人回購(GIT)克隆到服務器。Git Clone的Ansible和Git權限被拒絕(publickey)

我已設置ssh轉發,當我ssh進入服務器並嘗試從同一個repo手動克隆時,它成功工作。但是,當我使用ansible將克隆到服務器的repo時,它會以「Permission Denied Public Key」權限失敗。

這是我的劇本deploy.yml

​​

這是我ansible.cfg的樣子:

[ssh_args] 
ssh_args = -o FowardAgent=yes 

我也能執行我的劇本中的所有其他任務(OS操作,安裝) 。

我曾嘗試:在服務器上

  • 指定sshAgentForwarding標誌ansible.cfg使用(在同一目錄作爲劇本ansible.cfg):

    ssh_args = -o ForwardingAgent =是

  • 使用become: false執行git克隆
  • 運行ansible -i devops/hosts webservers -a "ssh -T [email protected]"返回:

    an_ip_address | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true }

這是我用來運行劇本命令: 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": "[email protected]: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": []} 
+0

有時,當你更新你的Mac,你的關鍵停止工作(至少對我來說)。我不得不'ssh-add -K〜/ .ssh/id_rsa'然後它工作。 – Will

回答

4

通過閱讀文檔中ansible SSH轉發。我能夠找出解決方案。

問題是我的ssh密鑰沒有被轉發,因爲Ansible不會默認轉發你的密鑰,即使你已經設置了密鑰轉發~/.ssh/conf(我更新了我的問題,我修復之前我有ansible.cfg問題)。

該解決方案是transport = ssh從那裏ansible.cfg所在的位置添加到ansible.cfg[defaults]下加運行ansible-playbook

ansible.cfg現在看起來是這樣的:

[defaults] 
transport = ssh 

[ssh_connection] 
ssh_args = -o ForwardAgent=yes 
+0

我如何要求Ansible-playbook考慮這個文件。例如:'ansible-playbook -i「localhost,」-c local GitClone.yaml'。在哪裏以及如何包含cfg文件? – Dawny33

+1

一旦您運行'ansible-playbook -i主機.yml',cfg文件就會自動包含。您應該將cfg文件放在與您要運行的Playbook相同的目錄中。另外請確保在運行'ansible-playbook'之前運行'ssh-add'。 – lv10

3

要克隆的私人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 

現在,您可以克隆的github私人回購甚至是root用戶

通常情況下,我也加入這兩個任務在我的劇本/角色任務以及:

- 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 

希望這可能有助於你和他人

+0

感謝您花時間回覆。我嘗試了你的建議,但也沒有奏效。我能夠通過在'ansible.cfg'' [defaults]'中使用'transport = ssh'來使它工作。 – lv10

+0

@ lv10我添加了另一個選項,請檢查。 –

相關問題