在节点上运行任务,如果任务失败,则从不同节点重新启动deamon



其中一项任务是在节点[a,B,C]上运行curl命令,如果不存在特定字符串,则该命令将失败。如果失败,我想通过在应用程序[A,B,C]上重新启动应用程序来进行救援。nodeA===>appA。nodeB==>appB。nodeC===>appC

[group1]
nodeA
nodeB
nodeC
[group2]
appA
appB
appC

- hosts: group1
serial: 1    #I want to do it one at a time
tasks:
-name: find if app is running
command: curl example.com
register: curl_result
failed_when: 'version' not in curl_result
- hosts: group2
tasks:
- name: restart deamon
service: app_deamon
state: restarted

目前,这将重新启动group2下的所有deamon。

如果nodeA任务失败,那么针对正确的主机运行重新启动任务是很重要的。appA服务应该是重新启动的服务,而不是appB或appC。提前感谢您对此的任何帮助

您可以为清单中的每个主机添加一个主机变量,以指示其专用的"救援主机"。例如:

[group1]
nodeA rescue_host=appA
nodeB rescue_host=appB
nodeC rescue_host=appC

然后在你的游戏中,根据rescue_host变量委托救援任务:

- block:
- name: find if app is running
command: curl example.com
register: curl_result
failed_when: 'version' not in curl_result
rescue:
- <task>: <restart application>
delegate_to: "{{ rescue_host }}"

最新更新