In case task fails, how to terminate rest of the execution?

Viewed 92

I am running a complex playbook using shell in one of my Ansible playbook task. And I am running an upgrade for s/w using command module in one of my Ansible playbook tasks.

How do I terminate execution of rest of the playbook in case output of this command includes failed?

Currently its silently failing and rest of the playbook executing without alerting any failure.

Below is a part of main playbook which has multiple other tasks:

- hosts: abc
  tasks:
    - name: Upgrade S/W 
      include_role:
        name: sw.upgrade
      register: upgrade_result
      tags:
      - upgrade

    - debug:
        msg: "{{upgrade_result}}"

and below is task from role:

- name: Execute upgrade
  command: "running some upgrade command here"
  async: 7200
  poll: 10
  register: sw_upgrade_hosts

I am new to Ansible and tried failed_when option but that doesn't give expected result.

2 Answers

You can try using following logic :

For shell invoking in Ansible use bash script and can write all shell cmds there.

Next step is to invoke this bash script in ansible ..

- name: example of many failed
  ansible.builtin.shell: "./xyz.sh"
  register: ret
  failed_when: >
    ("Error string") or
    (ret.stderr != '') or
    (ret.rc == 10)

Something like this could be of help here!

Related