【问题标题】:git pull command using Ansible使用 Ansible 的 git pull 命令
【发布时间】:2019-12-27 11:38:19
【问题描述】:

谁能告诉我下面的ansible git模块和git命令有什么区别

当我在 playbook 中使用 git 模块时,它会覆盖它不应该覆盖的文件,而 git 命令不会覆盖文件

我试图将 GIT 命令翻译成剧本中的任务

ANSIBLE GIT 模块

- 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 命令

git pull origin 20190524v016 --tags

【问题讨论】:

    标签: git ansible


    【解决方案1】:

    ansible/modules/source_control/git.py中的代码很清楚:

        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)
    

    因此,预计会在“之前”git pull 部分收到此错误消息。

    has_local_mods() 函数是:

    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
    

    当我在 playbook 中使用 git 模块时,它会覆盖它不应该覆盖的文件,而 git 命令不会覆盖文件

    这是令人费解的部分:仔细检查git status --porcelain 输出(在命令行中完成时),看看你的文件是否真的被修改了。

    【讨论】:

      【解决方案2】:

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

      force=yes
      

      如果是,工作存储库中的任何修改文件都将被丢弃。在 0.7 之前,这始终是“是”并且无法禁用。在 1.9 之前,默认为 yes

      即,将force=no 设置为不覆盖文件。

      【讨论】:

      • 如果 force=no 设置为 fatal,playbook 将失败并出现以下错误:[]: FAILED! => {"before": "23300bf0eab215bc21cba285c2a43c39ba004b24", "changed": false, "msg": "存储库中存在本地修改(force=no)。"}
      • 似乎模块git 不允许通过修改拉入回购。我的建议是使用commandshell 模块。或者在拉取之前/之后存储/取消存储修改。
      猜你喜欢
      • 2014-02-06
      • 1970-01-01
      • 2016-02-08
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2023-03-14
      • 2016-12-31
      相关资源
      最近更新 更多