# How to correctly authenticate git user with SSH key

**URL:** <https://forum.ansible.com/t/how-to-correctly-authenticate-git-user-with-ssh-key/15717>\
**Category:** Ansible Project\
**Created:** [July 20, 2014, 10:55am UTC](https://forum.ansible.com/t/how-to-correctly-authenticate-git-user-with-ssh-key/15717 "2014-07-20T10:55:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![9devmail](https://avatars.discourse-cdn.com/v4/letter/9/779978/32.png) [@9devmail](https://forum.ansible.com/u/9devmail)\
**Post date:** [July 20, 2014, 10:55am UTC](https://forum.ansible.com/t/how-to-correctly-authenticate-git-user-with-ssh-key/15717/1 "2014-07-20T10:55:14Z")

</div>

I want to pull code from [Bitbucket.org](http://Bitbucket.org)

`

- copy: src=path/to/`mykey`.pub dest=/home/{{ username }}/.ssh/`mykey`.pub  
sudo: false

- copy: src=path/to/`mykey` dest=/home/{{ username }}/.ssh/`mykey` mode=0600  
sudo: false

- copy: src=path/to/known\_hosts dest=/home/{{ username }}/.ssh/known\_hosts  
sudo: false

- authorized\_key: user={{ username }} key=“{{ lookup(‘file’, ‘/home/{{username}}/.ssh/`mykey`.pub’) }}”

- git: repo={{ project\_repo }} dest={{ project\_code }} version={{ branch }} accept\_hostkey=true key\_file=“{{ lookup(‘file’, ‘/home/{{ username }}/.ssh/mykey.pub’) }}”

`

After running this playbook I get:

`  
…  
“cmd”: “/usr/bin/git ls-remote [git@bitbucket.org](mailto:git@bitbucket.org):/myusername/mypro.git -h refs/heads/develop”, “failed”: true  
…  
stderr: Permission denied (publickey).  
fatal: Could not read from remote repository.

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

`

NOTICE: While using bash, I can execute the “cmd” command without errors (even without sudo). My user logs in and connect to the repo. I do not understand why ansible is not able to repeat it.

---

<div class="post-metadata">

**Author:** ![Michael\_Vermaes](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@Michael\_Vermaes](https://forum.ansible.com/u/Michael_Vermaes)\
**Post date:** [July 20, 2014, 12:19pm UTC](https://forum.ansible.com/t/how-to-correctly-authenticate-git-user-with-ssh-key/15717/2 "2014-07-20T12:19:10Z")

</div>

Couple of things to check:

- Can you run the git command with ‘sudo: false’? See here for why: [https://help.github.com/articles/error-permission-denied-publickey#sudo-or-sudont](https://help.github.com/articles/error-permission-denied-publickey#sudo-or-sudont)
- If you need to run git with sudo, and that’s why you have specified key\_file=…, I think you want to use your private key (mykey), rather than your public key (mykey.pub).

Hope it helps.
