gpt4 book ai didi

ansible - 如何在 Ansible 中捕获失败并继续执行特定任务的剧本

转载 作者:行者123 更新时间:2023-12-05 03:50:01 25 4
gpt4 key购买 nike

我有一个剧本,其中包含三个特定任务。

- name: Execute the compilation script
command: sh {{ working_folder }}/compile.sh
args:
chdir: "{{ working_folder }}"
when: run_deploy_machine == "true"

- name: Execute the deployment script
command: sh {{ working_folder }}/deploy.sh
args:
chdir: "{{ working_folder }}"
when: run_deploy_machine == "true"

- name: Start the JBoss server
shell: . /jboss start

问题是,如果前两个任务中的任何一个失败,我需要(作为失败过程的一部分)激活最后一个任务的逻辑(它可能作为处理程序)。我看到有阻止/救援选项,问题是如果我使用它 - 救援“取消”失败。我只需要在失败的情况下执行启动 JBoss,但 playbook 仍然会失败。

有什么想法可以做到吗?

最佳答案

您仍然可以使用阻止/救援并在救援任务结束时使用失败任务。这是一个全局性的想法:

---
- name: Clean fail in rescue demo
hosts: localhost
gather_facts: false

tasks:

- block:

- name: task that may fail
command: /bin/false

- name: other task that might fail
command: /does/this/work

rescue:

- name: task to remedy fail
command: echo remedy

- name: cleanly fail the host anyway
fail:
msg: clean fail after remedy

关于ansible - 如何在 Ansible 中捕获失败并继续执行特定任务的剧本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63683250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com