'Unable to perform "git clone" on local system using Ansible

I am trying to clone a git repo on my local system. I have done this manually and it works fine, but when I try to do it via Ansible, it doesn't work out

Here is my play:

---
  - name: Create a directory on root
    file:
      path: "{{ local_home_dir }}/superb-queue"
      owner: "{{ local_user }}"
      group: "{{ local_user }}"
      state: directory
    delegate_to: localhost

  - name: Clone the bitbucket queue repo locally
    git:
      repo: [email protected]:superbhq/queue-main.git
      dest: "{{ local_home_dir }}/superb-queue"
      clone: yes
      recursive: yes
      force: yes
      accept_hostkey: yes
      version: master
      key_file: "{{ local_home_dir }}/.ssh/id_rsa"
    become_user: "{{ local_user }}"
    delegate_to: localhost

The error I get is:

ASK [deploy-queue-main : Clone the bitbucket queue repo locally] ******************************************************************************************************************************************
fatal: [10.0.3.219 -> localhost]: FAILED! => {"changed": false, "cmd": "/usr/bin/git clone --origin origin '' /home/nishant/superb-queue", "msg": "fatal: destination path '/home/nishant/superb-queue' already exists and is not an empty directory.", "rc": 128, "stderr": "fatal: destination path '/home/nishant/superb-queue' already exists and is not an empty directory.\n", "stderr_lines": ["fatal: destination path '/home/nishant/superb-queue' already exists and is not an empty directory."], "stdout": "", "stdout_lines": []}
fatal: [10.0.4.36 -> localhost]: FAILED! => {"changed": false, "cmd": "/usr/bin/git clone --origin origin '' /home/nishant/superb-queue", "msg": "Cloning into '/home/nishant/superb-queue'...\nWarning:********@bitbucket.org: 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.", "rc": 128, "stderr": "Cloning into '/home/nishant/superb-queue'...\nWarning: Permanently added 'bitbucket.org,104.192.143.3' (RSA) to the list of known hosts.\r\[email protected]: 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", "stderr_lines": ["Cloning into '/home/nishant/superb-queue'...", "Warning: Permanently added 'bitbucket.org,104.192.143.3' (RSA) to the list of known hosts.", "[email protected]: 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": []}

The directory is empty on the localsystem, and I have the right keys. Not sure why this is happening



Solution 1:[1]

You have two errors:

  • path '/home/nishant/superb-queue' already exists and is not an empty directory
    If you already cloned your repo manually, make sure to delete that cloned repository folder before attempting it again through Ansible.
  • [email protected]: Permission denied (publickey).
    Make sure Ansible is running with the same account as yours, in order to use the same SSH keys in ~/.ssh.
    Or define the right ansible.cfg.

As mentioned in issue 5722, try:

- name: Clone code repository
  git:  repo=example.com/repos/enterprise.git
        dest=/home/user/enterprise
        accept_hostkey=yes
        force=yes
        recursive=no
        key_file={{ userhome }}/.ssh/id_rsa
        depth={{ repo_depth }}

Here, dest should be the (non-existing) root folder of the cloned repository (not ~, but ~/myrepo).

Solution 2:[2]

You are running the task concurrently on multiple host targets, each of which is delegating to the local host, effectively competing with each other:

fatal: [10.0.3.219 -> localhost]: ...
fatal: [10.0.4.36 -> localhost]: ...

Add run_once: true to the task.

Solution 3:[3]

From your question, I assume that you are using public repo if not then you should have added key_file and you probably need to add a user also.

Please have a look at the below solution for more info https://stackoverflow.com/a/39735848/9857025

Let us know if this helped.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eddie C.
Solution 2 techraf
Solution 3