首页 > 解决方案 > 错误!“重试”不是 TaskInclude 的有效属性

问题描述

我的要求是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

但是,运行剧本时出现以下错误。

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

你能建议吗?

标签: ansiblepsretry-logicuntil-loop

解决方案


首先,更正中任务的缩进inner.yml。其次,删除retriesdelayfromuntil并将stop.yml它们移动到特定任务,因为这些是任务级参数。

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

由于 stdout_lines 将包含字符串列表和版本需要 int 因此转换。

内部.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', '<')

停止.yml

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

推荐阅读