安全 |在多个服务器上异步重试任务 运行
Ansible | Making a retrying task run asynchronously on multiple servers
我有一个任务 运行s 并重试 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'
这可能吗?
将 strategy 设置为 free
:
- hosts: all
strategy: free
tasks:
...
这将指示 Ansible 尽快运行每台主机上的任务,而无需等待一组中的其他主机。
我有一个任务 运行s 并重试 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'
这可能吗?
将 strategy 设置为 free
:
- hosts: all
strategy: free
tasks:
...
这将指示 Ansible 尽快运行每台主机上的任务,而无需等待一组中的其他主机。