Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch a failure in Ansible and continue the playbook for specific task

Tags:

ansible

I have a playbook tat contains (Among other) three specfic tasks.

- 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

THe problem is that if any of the first two tasks fails, I need (As part of the failure process) activate the logic of the last task (It might be as a handler). I saw that there is the block/rescue option, the problem is that if I use it- the rescue "cancel" the failure. All I need is that in case of the failure- to execute the start JBoss, but that the playbook will still fail.

Any ideas how it can be done?

like image 717
Eyal Goren Avatar asked Sep 09 '25 14:09

Eyal Goren


1 Answers

You can still use a block/rescue and use a fail task at the end of the rescue tasks. Here is a global idea:

---
- 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
          
like image 137
Zeitounator Avatar answered Sep 13 '25 18:09

Zeitounator