Ansible and Git Permission denied (publickey) at Git Clone
<p>I have a playbook where I am trying to clone from a private repo (GIT) to a server. </p>

<p>I have setup ssh forwarding and when I ssh into the server and try to manually clone from the same repo, it successfully works. However, when I use ansible for the to clone the repo to the server, it fails with "Permission Denied Public Key". </p>

<p>This is my playbook <code>deploy.yml</code>:</p>

<pre><code>---

- hosts: webservers
  remote_user: root

  tasks:
      - name: Setup Git repo
        git: repo={{ git_repo }}
             dest={{ app_dir }}
             accept_hostkey=yes
</code></pre>

<p>This is how my <code>ansible.cfg</code> looks:</p>

<pre><code>[ssh_args]
ssh_args = -o FowardAgent=yes
</code></pre>

<p>I am also able to perform all the other tasks in my playbooks (os operations, installations).</p>

<p>I have tried:</p>

<ul>
<li>Specifying sshAgentForwarding flag in <code>ansible.cfg</code> on the server (ansible.cfg in same dir as playbook) using:

<blockquote>
  <p>ssh_args = -o ForwardingAgent=yes</p>
</blockquote></li>
<li>used <code>become: false</code> to execute the git clone</li>
<li><p>running <code>ansible -i devops/hosts webservers -a "ssh -T git@bitbucket.org"</code> returns:</p>

<p><code>an_ip_address | UNREACHABLE! =&gt; {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}</code></p></li>
</ul>

<p>This is the command that I use to run the playbook:
<code>ansible-playbook devops/deploy.yml -i devops/hosts -vvvv</code>
This is the error message I get:</p>

<pre><code>fatal: [162.243.243.13]: 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": "/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": []}
</code></pre>