我有一个任务,该任务运行并重新检索shell命令,直到某个字符串不在日志文件的最后一行中为止。它每秒重试500次。
遇到条件时,它将关闭服务器。当使用serial: 1
运行一台服务器时,这效果很好。
但是,我想一次在多个服务器上运行。问题在于,它将等待所有服务器在关闭所有服务器之前符合条件。
我希望它不同步运行并在该服务器符合条件时关闭每个服务器。
我已经尝试了async: true
和poll: 0
,但是在任务中使用retry: true
时,这似乎不起作用。
这是一个代码片段,可以让您了解我在做什么。
- name: "Checking for inactivity on the server before shutdown"
shell: "tail -1 serverlogs.log | egrep 'STRING1|STRING2' ||:"
args:
chdir: "/path/to/log/"
register: check_inactivity
until: '"STRING1" not in check_inactivity.stdout and "STRING2" not in check_inactivity.stdout'
retries: 500
delay: 1
- name: Debug message
debug:
msg: "Shutting down the server | {{ inventory_hostname }}"
when: 'check_inactivity | changed'
这就是我想实现的
- name: "Checking for inactivity on the server before shutdown"
shell: "tail -1 serverlogs.log | egrep 'STRING1|STRING2' ||:"
args:
chdir: "/path/to/log/"
register: check_inactivity
until: '"STRING1" not in check_inactivity.stdout and "STRING2" not in check_inactivity.stdout'
retries: 500
delay: 1
async: 45
poll: 0
- name: Debug message
debug:
msg: "Shutting down the server | {{ inventory_hostname }}"
when: 'check_inactivity | changed'
这是可能的吗?
将策略设置为 free
:
- hosts: all
strategy: free
tasks:
...
这将指示Ansible可以尽快在每个主机上运行任务,而无需等待集合中的其他主机。