安塞 bool 错误! 'retries' 不是任务包含的有效属性

标签 ansible ps retry-logic until-loop

我的要求是运行脚本 stop-all 多次(重试 5 次),直到 ps -fu user1 |wc -l 的输出小于 2 .

我为此编写了以下 ansible 剧本:

cat stop.yml

  - hosts: dest_nodes
    tasks:
      - name: Start service
        include_tasks: "{{ playbook-dir }}/inner.yml"
        retries: 5
        delay: 4
        until: stopprocesscount.stdout is version('2', '<')


cat inner.yml

      - name: Start service
          shell: ~/stop-all
          register: stopprocess

      - name: Start service
          shell: ps -fu user1 |wc -l
          register: stopprocesscount

但是,我在运行 playbook 时遇到以下错误。

ERROR! 'retries' is not a valid attribute for a TaskInclude

The error appears to be in '/app/playbook/stop.yml': line 19, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


      - name: Start service
        ^ here

你能推荐一下吗?

最佳答案

首先,更正 inner.yml 中任务的缩进。其次,从 stop.yml 中删除 retriesdelayuntil 并将它们移至特定任务,因为这些是任务电平参数。

由于您需要基于另一任务重试一项任务,因此您可以将脚本和命令结合起来并提取 wc -l 命令的结果,如下所示:

由于 stdout_lines 将包含字符串列表,并且版本需要 int,因此需要进行转换。

inner.yml

  - name: Start service
    shell: ~/stop-all; ps -fu user1 | wc -l
    register: stopprocesscount
    retries: 5
    delay: 4
    until: stopprocesscount.stdout_lines[stopprocesscount.stdout_lines | length - 1] | int  is version('2', '<')

stop.yml

  - hosts: dest_nodes
    tasks:
      - name: Start service
        include_tasks: "{{ playbook-dir }}/inner.yml"

关于安塞 bool 错误! 'retries' 不是任务包含的有效属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61925308/

相关文章:

python - 为什么可以在ansible中使用分割过滤器?

caching - 将事实收集到事实缓存的最快方法

ansible - 使用 ansible 插入 etc 主机文件

Linux - 创建进程

regex - 如何在 Ansible 的 lineinfile 模块中的正则表达式中转义 1 个或多个空格?

ubuntu - 如何使用系统启动运行软件 - ubuntu

linux - 让(父)进程在 Linux shell 中执行命令

python - 重试失败的 sqlalchemy 查询

swift - 为什么在 retryWhen 计时器结束时使用 take(1)?

java - 如何为具有重试逻辑的方法编写junit测试用例