Reputation: 185
My git workspace is dirty, there are some local modifications. When I use command git pull origin master
it works fine because there is no conflict.
But when I'm trying to use Ansible
like
git: repo=xxxx dest=xxx version={{branch}}
I got error:
Local modifications exist in repository (force=no)
If I add force=yes
, then I will lose my local modifications.
What can I do to keep my local changes and pull latest commit from git by using Ansible git module.
Upvotes: 17
Views: 19831
Reputation: 51
You can use stash/unstash functionality before/after cloning the repo along with the force: yes
- name: Stash git changes
ansible.builtin.shell:
cmd: "git stash"
chdir: "/home/{{ common_user }}/nginx"
- name: Clone a Nginx repo
ansible.builtin.git:
repo: "{{ repo_url }}"
dest: "/home/{{ common_user }}/nginx"
force: yes
- name: Unstash git changes
ansible.builtin.shell:
cmd: "git stash pop"
chdir: "/home/{{ common_user }}/nginx"
register: unstash_output
failed_when:
- unstash_output.failed
- not 'No stash entries found' in unstash_output.stderr
Tested with ansible version - 2.13.2
Upvotes: 0
Reputation: 317
You can achieve this by keeping the Ansible git module and ignoring this specific error message when it happens:
- name: Clone repo
git:
repo: "{{ repo_url }}"
dest: "{{ repo_dest }}"
register: repo_clone
failed_when:
- repo_clone.failed
- not 'Local modifications exist in repository' in repo_clone.msg
This task will fail either if the repo clone has failed, or another string than 'Local modifications exist in repository' is found in the fail message.
Upvotes: 5
Reputation: 68559
You cannot achieve it using the git module.
Ansible checks the result of:
git status --porcelain
and aborts task execution if there are local changes in tracked files, unless force
parameter is set to true
.
Upvotes: 15
Reputation: 171
You can use something like :
git: repo=xxxx dest=xxx version={{branch}} force=no
ignore_errors: yes
Upvotes: 3