So today I spent some time fiddling with my playbooks for deploying my blog. I ran into this little gem while trying to use ansible to clone the gitlab repository I sync to the server.

Gitlab/hub provide the URLs in the repo like this.

ssh://git@gitlab.com:caphrim007/blog.git

This actually doesn’t work though, because the URL is technically incorrect. For example, with the above URL, I would get the following error.

Perform task: blog | Clone the source code (y/n/c):  **************************
<localhost> REMOTE_MODULE git dest=cache/blog version=master force=yes repo=ssh:********@gitlab.com:caphrim007/blog.git accept_hostkey=True
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1418080918.08-129911124351692 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1418080918.08-129911124351692 && echo $HOME/.ansible/tmp/ansible-tmp-1418080918.08-129911124351692']
<localhost> PUT /var/folders/jc/9d1188j962931rhqrlm4173w5j5m45/T/tmpDTluSd TO /Users/trupp/.ansible/tmp/ansible-tmp-1418080918.08-129911124351692/git
<localhost> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /Users/trupp/.ansible/tmp/ansible-tmp-1418080918.08-129911124351692/git; rm -rf /Users/trupp/.ansible/tmp/ansible-tmp-1418080918.08-129911124351692/ >/dev/null 2>&1']
failed: [caphrim.net -> localhost] => {"cmd": "/usr/bin/git ls-remote ssh:********", "failed": true, "rc": 128}
stderr: X11 forwarding request failed on channel 0
Access denied.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

msg: X11 forwarding request failed on channel 0
Access denied.
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

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/trupp/deploy-blog.yaml.retry

caphrim.net                : ok=2    changed=0    unreachable=0    failed=1

SEA-ML-RUPP1:caphrim-automation trupp$

So what is the correct format of the URL? Well, it looks like this.

ssh://git@gitlab.com/caphrim007/blog.git

Notice the forward slash after the hostname? The smoking gun!

Changing it to the correct URL format, resulted in working code

Perform task: blog | Clone the source code (y/n/c):  **************************
<localhost> REMOTE_MODULE git dest=cache/blog version=master force=yes repo=ssh:********@gitlab.com/caphrim007/blog.git accept_hostkey=True
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1418081327.1-148483259248905 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1418081327.1-148483259248905 && echo $HOME/.ansible/tmp/ansible-tmp-1418081327.1-148483259248905']
<localhost> PUT /var/folders/jc/9d1188j962931rhqrlm4173w5j5m45/T/tmp6FXFYO TO /Users/trupp/.ansible/tmp/ansible-tmp-1418081327.1-148483259248905/git
<localhost> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /Users/trupp/.ansible/tmp/ansible-tmp-1418081327.1-148483259248905/git; rm -rf /Users/trupp/.ansible/tmp/ansible-tmp-1418081327.1-148483259248905/ >/dev/null 2>&1']
changed: [caphrim.net -> localhost] => {"after": "385a29df6cd3b873e207a3288a6d607b77b9910c", "before": null, "changed": true}

And the correct working code in Ansible looks like this.

Working
  - name: Clone the source code
  git: dest=blog
       version=master
       force=yes
       repo=ssh://git@gitlab.com/caphrim007/blog.git
       accept_hostkey=True
  delegate_to: localhost