简体   繁体   中英

git pull command using Ansible

Can anyone please advice me what is the difference between the following ansible git module and git command

while i'm using the git module in playbook its overwriting a file which it shouldn't, while the git command does not overwrite the file

I have tried to translate the GIT command into a task in the playbook

ANSIBLE GIT MODULE

- name: pull v016 repo from git
    git: repo=https://{{ githubuser }}:{{ githubpassword|urlencode }}@git.abc.com/abc/abc.git
         dest=/tmp/test/abc
         accept_hostkey=yes
         update=yes
         clone=no
         force=yes
         remote=origin
         version=20190524v016

GIT COMMAND

git pull origin 20190524v016 --tags

The code in ansible/modules/source_control/git.py is clear:

    else:
        # else do a pull
        local_mods = has_local_mods(module, git_path, dest, bare)
        result['before'] = get_version(module, git_path, dest)
        if local_mods:
            # failure should happen regardless of check mode
            if not force:
                module.fail_json(msg="Local modifications exist in repository (force=no).", **result)

So getting this error message on the "before" git pull part is expected.

The has_local_mods() function is:

def has_local_mods(module, git_path, dest, bare):
    if bare:
        return False

    cmd = "%s status --porcelain" % (git_path)
    rc, stdout, stderr = module.run_command(cmd, cwd=dest)
    lines = stdout.splitlines()
    lines = list(filter(lambda c: not re.search('^\\?\\?.*$', c), lines))

    return len(lines) > 0

While I'm using the git module in playbook its overwriting a file which it shouldn't, while the git command does not overwrite the file

That is the puzzling part: double-check the git status --porcelain output (when done in command-line), to see if your file is actually modified or not.

https://docs.ansible.com/ansible/latest/modules/git_module.html#git-module

force=yes

If yes, any modified files in the working repository will be discarded. Prior to 0.7, this was always 'yes' and could not be disabled. Prior to 1.9, the default was yes

Ie, set force=no to not overwrite the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM