我的要求是运行脚本 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
中删除 retries
、delay
和 until
并将它们移至特定任务,因为这些是任务电平参数。
由于您需要基于另一任务重试一项任务,因此您可以将脚本和命令结合起来并提取 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/