我的要求是多次运行脚本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
你能建议一下吗?
首先,更正inner.yml
中任务的缩进。其次,从stop.yml
中删除retries
、delay
和until
,并将它们移动到特定任务,因为这些是任务级别的参数。
由于您需要基于另一个任务重试一个任务,因此您只需将脚本和命令组合在一起并提取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"
并非所有任务属性都适用于所有任务(此处为 TaskInclude 任务(。
没有明确的文档,这样的兼容性矩阵,但这里的错误消息非常清楚"不是有效的属性"。
按实例:
你不能循环一个块:'with_items' is not a valid attribute for a Block
不能异步任务包含,请参阅 https://github.com/ansible/ansible/issues/57051。