【问题标题】:Combine Ansible async with retries将 Ansible 异步与重试相结合
【发布时间】:2020-02-08 22:15:32
【问题描述】:

好的,现在我遇到了一个非常棘手的 Ansible 案例。 我需要通过重试异步运行我的任务(即使用直到循环),然后如果超时超过则任务失败。所以两个参数都必须控制我的播放:重试次数(如果超过重试次数,播放失败)和超时(如果超过超时,播放失败)。

我可以分别实施每个策略:

- name: My shell task with retries
  shell: set -o pipefail && ./myscript.sh 2>&1 | tee -a "{{mylogfile}}"
  args:
    chdir: "{{myscript_dir}}/"
    executable: /bin/bash
  register: my_job
  until: my_job is succeeded
  retries: "{{test_retries}}"
  delay: 0

或异步:

- name: My async shell task
  shell: set -o pipefail && ./myscript.sh 2>&1 | tee -a "{{mylogfile}}"
  args:
    chdir: "{{myscript_dir}}/"
    executable: /bin/bash
  register: my_job
  async: "{{test_timeout}}"
  poll: 0

- name: Tracking for async shell task
  wait_for:
    path: "{{mylogfile}}"
    search_regex: '^.*Done in \S+'
    timeout: "{{test_timeout}}"
  ignore_errors: yes
  register: result

第二个任务解析前一个任务日志,直到作业完成 - 即搜索“在 x 秒内完成”字符串。也许这不是最佳实践,我应该使用async_status,但找不到如何设置超时(它只有重试检查作业状态,这对我来说很愚蠢)。

太好了...我可以将这两种策略结合起来,通过重试次数和超时来控制我的任务吗?

UPD:我尝试为shell 模块运行untilasync,令人惊讶的是我的播放没有失败,但重试不起作用。该任务刚刚作为即发即弃的任务启动,并且只执行一次而没有重试。所以这不是一个选择。

- name: My shell task with retries and async
  shell: set -o pipefail && ./myscript.sh 2>&1 | tee -a "{{mylogfile}}"
  args:
    chdir: "{{myscript_dir}}/"
    executable: /bin/bash
  register: my_job
  until: my_job is succeeded
  retries: "{{test_retries}}"
  delay: 0
  async: "{{test_timeout}}"
  poll: 0

- name: My async shell task
  wait_for:
    path: "{{mylogfile}}"
    search_regex: '^.*Done in \S+'
    timeout: "{{test_timeout}}"
  ignore_errors: yes
  register: result

【问题讨论】:

    标签: loops asynchronous ansible


    【解决方案1】:

    您是否尝试过同时使用重试和延迟?

    - name: Checking the build status 1
      async_status:
        jid: "{{ job1.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 10
      delay: 1
      when: job1.ansible_job_id is defined
    

    这将以 1 秒的间隔检查异步状态 10 次,如果在 10 秒内“直到”条件不满足,它将失败

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多