Reputation: 277
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?
Upvotes: 4
Views: 9949
Reputation: 4584
You could do something like that:
- name: Execute the compilation script
command: sh {{ working_folder }}/compile.sh
args:
chdir: "{{ working_folder }}"
when: run_deploy_machine == "true"
register: a
ignore_errors: True
- name: Execute the deployment script
command: sh {{ working_folder }}/deploy.sh
args:
chdir: "{{ working_folder }}"
when: run_deploy_machine == "true"
register: b
ignore_errors: True
- name: Start the JBoss server
shell: . /jboss start
when: a.rc != "0" or b.rc != "0"
- name: fail if build or deploy failed
fail:
msg: "Build or deploy failed!"
when: a.rc != "0" or b.rc != "0"
You register the variables a
and b
to contain the output of the commands. rc
will contain the exit code, so if that is not 0
the command failed.
If one of those commands failed, you execute task no. 3 and then fail.
ignore_errors: True
prevents the play to fail instantly if one of the first steps fail.
Documentation:
shell
error handling
fail
Upvotes: 0
Reputation: 44808
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
Upvotes: 5