title: How to forward ssh key in ansible to checkout git repository on target machine from origin?
body: <p>I'm trying to configure ansible to checkout a git repository from bitbucket and put it on the target machine. The control machine (my PC) has the bitbucket private ssh key. The public key is uploaded to bitbucket and ssh access with it is tested and working.</p>
  
  <p>Here's the ansible yml task code:</p>
  
  <pre><code>- name: Checkout application
    become: no
    git: repo=git@bitbucket.org:bitbucketusername/deployment.git
         dest=/tmp/myapp
         accept_hostkey=True
         key_file=/home/me/.ssh/bitbucket_ssh_key
  </code></pre>
  
  <p>Here's the error:</p>
  
  <pre><code>Warning: Identity file /home/me/.ssh/bitbucket_ssh_key not accessible: 
  No such file or directory.
  Permission denied (publickey)
  </code></pre>
  
  <p>So I take it that key forwarding is not working? This is odd, because in my <code>~/.ssh/config</code> I have forwarding enabled for the target machine 111.222.333.444: </p>
  
  <pre><code>Host 111.222.333.444
    ForwardAgent yes
  </code></pre>
  
  <p>So what's going wrong and how do I get my repo from bitbucket onto my target machine? I'm using ansible 2.1.1.0.</p>
  
  <p>Edit: here's the part where it complains when the <code>-vvvv</code> flag is on:</p>
  
  <pre><code>TASK [Checkout application] *********************************************
  task path: /home/me/path/to/the/ansible/playbook.yml:49
  &lt;111.222.333.444&gt; ESTABLISH SSH CONNECTION FOR USER: deploy
  &lt;111.222.333.444&gt; SSH: EXEC ssh -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o 'IdentityFile="/home/me/.ssh/the_ssh_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=deploy -o ConnectTimeout=10 111.222.333.444 '/bin/sh -c '"'"'( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1477655760.17-42684399995480 `" &amp;&amp; echo ansible-tmp-1477655760.17-42684399995480="` echo $HOME/.ansible/tmp/ansible-tmp-1477655760.17-42684399995480 `" ) &amp;&amp; sleep 0'"'"''
  &lt;111.222.333.444&gt; PUT /tmp/tmp0NYGtg TO /home/deploy/.ansible/tmp/ansible-tmp-1477655760.17-42684399995480/git
  &lt;111.222.333.444&gt; SSH: EXEC sftp -b - -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o 'IdentityFile="/home/me/.ssh/the_ssh_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=deploy -o ConnectTimeout=10 '[111.222.333.444]'
  &lt;111.222.333.444&gt; ESTABLISH SSH CONNECTION FOR USER: deploy
  &lt;111.222.333.444&gt; SSH: EXEC ssh -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o 'IdentityFile="/home/me/.ssh/the_ssh_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=deploy -o ConnectTimeout=10 -tt 111.222.333.444 '/bin/sh -c '"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/deploy/.ansible/tmp/ansible-tmp-1477655760.17-42684399995480/git; rm -rf "/home/deploy/.ansible/tmp/ansible-tmp-1477655760.17-42684399995480/" &gt; /dev/null 2&gt;&amp;1 &amp;&amp; sleep 0'"'"''
  fatal: [app1]: FAILED! =&gt; {"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": "/tmp/myapp", "executable": null, "force": false, "key_file": "/home/me/.ssh/bitbucket_ssh_key", "recursive": true, "reference": null, "refspec": null, "remote": "origin", "repo": "git@bitbucket.org:memeares/deployment.git", "ssh_opts": null, "track_submodules": false, "update": true, "verify_commit": false, "version": "HEAD"}, "module_name": "git"}, "msg": 
  "Warning: Identity file /home/me/.ssh/bitbucket_ssh_key not accessible: No such file or directory.
  Permission denied (publickey).
  fatal: Could not read from remote repository.
  Please make sure you have the correct access rights
  and the repository exists.", "rc": 128, "stderr": "Warning: Identity file /home/me/.ssh/bitbucket_ssh_key not accessible: No such file or directory.
  Permission denied (publickey).
  fatal: Could not read from remote repository.
  Please make sure you have the correct access rights
  and the repository exists.
  ", "stdout": "", "stdout_lines": []}
  </code></pre>
  
  <p>And I've also got forwarding configured in the ansible.cfg file (as visible in the above output):</p>
  
  <pre><code>[ssh_connection]
  # Enable SSH Agent Forwarding so that the private key used to be able to
  # checkout from git does not have to be on the server
  ssh_args=-o ForwardAgent=yes
  </code></pre>