Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible : Git module clone of repository while re-run is throwing exception

I am using ansible-playbook to setup server, which is cloning the repo from github. While I am re-running the play-book, I am getting the exception as msg: fatal: destination path '/webapps/........' already exists and is not an empty directory.

TASK: [web | Setup the Git repo] **********************************************
failed: [192.168.1.96] => {"cmd": "/usr/bin/git clone --origin origin --branch master https://github.com/....../......git /webapps/....../...../....", "failed": true, "rc": 128}
stderr: fatal: destination path '/webapps/..../..../....' already exists and is not an empty directory.

msg: fatal: destination path '/webapps/..../..../....' already exists and is not an empty directory.

FATAL: all hosts have already failed -- aborting

PLAYBOOK

- name: Setup the Git repo
  git: repo={{ git_repo }}
       version="{{ git_branch }}"
       dest={{ project_path }}
       accept_hostkey=yes
       force=yes
  when: setup_git_repo is defined and setup_git_repo
  tags: git

- name: Delete all .pyc files
  command: find . -name '*.pyc' -delete
  args:
    chdir: "{{ project_path }}"
  tags: git

How do I skip this step (or) over-write the files, if files are already cloned and exists in server.?.

like image 645
Remis Haroon - رامز Avatar asked Jan 16 '16 17:01

Remis Haroon - رامز


People also ask

How do I clone a Git repository in Ansible?

Cloning a Git Repository with Ansible playbook In the playbook above, you started by defining a new task and gave it the name “Clone a GitHub repository". Next, you are using the git module to specify the link to the SQLite GitHub repository. You then proceed to define the destination for the cloned repository.

What happens when you Git clone?

The Git clone command will create a new local directory for the repository, copy all the contents of the specified repository, create the remote tracked branches, and checkout an initial branch locally. By default, Git clone will create a reference to the remote repository called origin .


2 Answers

I am still having the issue and figured out the following (hacky) workaround:

- name: Setup the Git repo
  git: repo={{ git_repo }}
       version="{{ git_branch }}"
       dest={{ project_path }}
       accept_hostkey=yes
       force=yes
  when: setup_git_repo is defined and setup_git_repo
  tags: git
  ignore_errors: yes
  register: clone_command_res
  
- name: Only allow the expected failure
  assert:
    that: "not clone_command_res['failed'] or 'already exists and is not an empty directory' in clone_command_res['msg'].lower()"

In the later, it is possible that the error message can vary, depending upon your previous status (eg my error message is Local modifications exist in repository) or worse, git and/or ansible versions. In any case, just include the expected error messages (kind of like a try/catch).

Although it would be much nicer if there was an option for this case in ansible git module, this should do it for now.

like image 156
pavlaras Avatar answered Oct 02 '22 15:10

pavlaras


Why do not check if the directory exists, and if yes, do not clone it (but maybe pull it ?)

like image 35
NRE Avatar answered Oct 02 '22 14:10

NRE