Ansible:重新运行时Git模块克隆的存储库抛出异常

时间:2016-01-16 17:15:27

标签: ansible ansible-playbook

我正在使用ansible-playbook来设置服务器,它正在从github克隆repo。 当我重新运行游戏书时,我将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

如果文件已经克隆并存在于服务器中,如何跳过此步骤(或)覆盖文件。?

3 个答案:

答案 0 :(得分:1)

我仍然遇到问题并想出了以下(hacky)解决方法:

- 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()"

稍后,错误消息可能会有所不同,具体取决于您之前的状态(例如,我的错误消息是 Local modifications exist in repository)或更糟的是,git 和/或 ansible 版本。在任何情况下,只需包含预期的错误消息(有点像 try/catch)。

虽然如果在 ansible git 模块中有针对这种情况的选项会更好,但现在应该这样做。

答案 1 :(得分:0)

为什么不检查目录是否存在,如果是,则不要克隆它(但可以拉它吗?)

答案 2 :(得分:-8)

为什么不在克隆之前删除文件夹呢? http://docs.ansible.com/ansible/shell_module.html

- shell: rm -fr /yourfolder