【发布时间】: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 模块运行until 和async,令人惊讶的是我的播放没有失败,但重试不起作用。该任务刚刚作为即发即弃的任务启动,并且只执行一次而没有重试。所以这不是一个选择。
- 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