Ansible:运行异步任务,如果失败:运行清理任务并重试第一个任务x次

Ansible:运行异步任务,如果失败:运行清理任务并重试第一个任务x次,ansible,Ansible,我想创建一个类似以下内容的程序流: - name: Clone git repository block: - git: repo: "{{ project_gitlab_repository }}" dest: "{{ project_build_path }}" async: 120 poll: 5 rescue: - file: path: "{{ project_buil

我想创建一个类似以下内容的程序流:

- name: Clone git repository
  block:
  - git:
      repo: "{{ project_gitlab_repository }}"
      dest: "{{ project_build_path }}"
    async: 120
    poll: 5
  rescue:
  - file:
      path: "{{ project_build_path }}"
      state: absent
  - ansible.builtin.command: /bin/false 
  retries: 3
  • 尝试异步任务
  • 如果失败,请运行清理任务。(在这种情况下,删除目标文件夹)
  • 返回步骤1(x次)
  • 上面的代码不起作用,似乎您可以“重试块”。 有可能在Ansible中实现类似的功能吗?

    我通过将以下代码放入文件“clone\u git\u repository.yaml”解决了这个问题(hacky)

    不是很优雅,但显然这是一种方法,直到您可以指定块的重试次数

    - block:
      - name: Clone git repository
        git:
          repo: "{{ project_gitlab_repository }}"
          dest: "{{ project_build_path }}"
        async: 120
        poll: 5
      rescue:
      - set_fact:
          retry_count: "{{ 0 if retry_count is undefined else retry_count | int + 1 }}"
      - fail:
          msg: maximum retries reached
        when: retry_count | int == 5
      - command: "rm -rf {{ project_build_path }}"
      - include_tasks: clone_git_repository.yml