如何捕获 Ansible 中的故障并继续执行特定任务的剧本

Eya*_*ren 4 ansible

我有一本剧本,其中包含(除其他外)三个具体任务。

- 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

Run Code Online (Sandbox Code Playgroud)

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

有什么想法可以做到吗?

Zei*_*tor 5

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

---
- 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
          
Run Code Online (Sandbox Code Playgroud)