Git clone failing only via Ansible. Working via cmdline.

25 views
Skip to first unread message

Mukuntha rajaa

unread,
Jun 21, 2020, 3:46:57 AM6/21/20
to ansible...@googlegroups.com
Hello,

When I git clone from cmdline, I am able to clone successfully. But when I do the same thing via, Ansible, something is going wrong.
git clone g...@gitlab.com:xxxx/yyyy.git
Cloning into 'yyyy'...
remote: Enumerating objects: 223, done.
remote: Counting objects: 100% (223/223), done.
remote: Compressing objects: 100% (164/164), done.
remote: Total 223 (delta 21), reused 175 (delta 6), pack-reused 0
Receiving objects: 100% (223/223), 667.18 KiB | 204.00 KiB/s, done.
Resolving deltas: 100% (21/21), done.
Also, following is fine. 
ssh -vT g...@gitlab.com
[...]
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Welcome to GitLab, @mukuntharajaa!
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype e...@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2776, received 3416 bytes, in 0.9 seconds
Bytes per second: sent 2926.2, received 3600.8
debug1: Exit status 0
Only via Ansible, git cloning is failing.

fatal: [192.168.99.105]: FAILED! => {"changed": false, "cmd": ["/usr/bin/git", "fetch", "--tags", "origin"], "msg": "Failed to download remote objects and refs:  Load key \"/home/mrajaa/.ssh/id_rsa.pub\": invalid format\r\ng...@gitlab.com: Permission denied (publickey).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n"}
My git clone task is like this,

name: use git to clone myconfigs
git:
dest: “{{ myconfig_dir }}”
repo: “{{ dotfile_repo }}”
accept_hostkey: yes
key_file: “{{ ssh_key }}”
force: no #this will not forcefully clone, if there are some local modifications
ignore_errors: true
```



I even have task to add ssh keys. 
  - name: Add ssh keys to ssh-agent
    authorized_key:
      user: "{{ username }}"
      state: present
      key: "{{ lookup('file', item) }}"
    with_fileglob:
      - "{{ home_dir }}.ssh/*.pub"

What is the right way to clone a private repository from gitlab/github using ssh keys ?

Thanks and Regards,
Mukuntha Rajaa


Stefan Hornburg (Racke)

unread,
Jun 21, 2020, 4:01:08 AM6/21/20
to ansible...@googlegroups.com
On 6/21/20 9:46 AM, Mukuntha rajaa wrote:
> Hello,
>
> When I git clone from cmdline, I am able to clone successfully. But when I do the same thing via, Ansible, something is
> going wrong.
>
> |git clone g...@gitlab.com:xxxx/yyyy.git Cloning into 'yyyy'... remote: Enumerating objects: 223, done. remote: Counting
> objects: 100% (223/223), done. remote: Compressing objects: 100% (164/164), done. remote: Total 223 (delta 21), reused
> 175 (delta 6), pack-reused 0 Receiving objects: 100% (223/223), 667.18 KiB | 204.00 KiB/s, done. Resolving deltas: 100%
> (21/21), done. |
>
> Also, following is fine. 
>
> |ssh -vT g...@gitlab.com <mailto:g...@gitlab.com> [...] debug1: Sending environment. debug1: Sending env LANG =
> en_US.UTF-8 *Welcome to GitLab*, @mukuntharajaa! debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
> debug1: client_input_channel_req: channel 0 rtype e...@openssh.com <mailto:e...@openssh.com> reply 0 debug1: channel 0:
> free: client-session, nchannels 1 Transferred: sent 2776, received 3416 bytes, in 0.9 seconds Bytes per second: sent
> 2926.2, received 3600.8 debug1: Exit status 0 |
>
> Only via Ansible, git cloning is failing.
>
> |fatal: [192.168.99.105]: FAILED! => {"changed": false, "cmd": ["/usr/bin/git", "fetch", "--tags", "origin"], "msg":
> "Failed to download remote objects and refs: ||Load key \"/home/mrajaa/.ssh/id_rsa.pub\": *invalid format\r\ng...@gitlab.com <mailto:ng...@gitlab.com>: Permission
> denied (publickey)*.\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access
> rights\nand the repository exists.\n"} |
>
> My git clone task is like this,
> ------------------------------------------------------------------------------------------------------------------------
>
> name: use git to clone myconfigs
> git:
> dest: “{{ myconfig_dir }}”
> repo: “{{ dotfile_repo }}”
> accept_hostkey: yes
> key_file: “{{ ssh_key }}”
> force: no #this will not forcefully clone, if there are some local modifications
> ignore_errors: true
> ```
>

The value for key_file expects the path to the *private* SSH key.

Regards
Racke

>
>
> I even have task to add ssh keys. 
>
> |- name: Add ssh keys to ssh-agent authorized_key: user: "{{ username }}" state: present key: "{{ lookup('file', item)
> }}" with_fileglob: - "{{ home_dir }}.ssh/*.pub" |
>
>
> What is the right way to clone a private repository from gitlab/github using ssh keys ?
>
> Thanks and Regards,
> Mukuntha Rajaa
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAD2erzcG76j4YMb-%2B0ngQrKqEgK%3DRSbb4WYfurszZQTm9w7smw%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAD2erzcG76j4YMb-%2B0ngQrKqEgK%3DRSbb4WYfurszZQTm9w7smw%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Dick Visser

unread,
Jun 21, 2020, 4:47:21 AM6/21/20
to ansible...@googlegroups.com
Note that your task name is a bit misleading, as it add keys to a file. Nothing is done with agents. 


What is the right way to clone a private repository from gitlab/github using ssh keys ?

Thanks and Regards,
Mukuntha Rajaa


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAD2erzcG76j4YMb-%2B0ngQrKqEgK%3DRSbb4WYfurszZQTm9w7smw%40mail.gmail.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Mukuntha rajaa

unread,
Jun 21, 2020, 6:10:14 AM6/21/20
to Ansible Project
Thanks. That worked !!! I missed to see this word in document. My bad :(
"

Specify an optional private key file path, on the target host, to use for the checkout.
"
Reply all
Reply to author
Forward
0 new messages