我的要求是将脚本stop-all
运行多次(重试5次,直到ps -fu user1 |wc -l
的输出小于2。
我为同一本书写了以下有趣的剧本:
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
中任务的缩进。其次,从retries
中删除delay
,until
和stop.yml
并将它们移至特定任务,因为它们是任务级别参数。
由于您需要基于另一个任务重试一个任务,因此您可以将脚本和命令组合在一起并提取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"