Dan.
Dan.

Reputation: 727

Is there a way to run tasks on failure without block/rescue?

I'd like to run some code (e.g. a task) when a task fails. The only way I've found to do this is using blocks.

The block/rescue method works for my use case, but it's forcing an (otherwise) unnecessary keyword in my playbook.

Is there a way to rewrite the below:

- name: my play
  hosts: 127.0.0.0
  any_errors_fatal: true
  connection: local
  tasks:
    - name: install something and configure that thing
      block:
        - name: install something
          shell: install something
        - name: configure that something
          shell: do some things
      rescue:
        - name: rollback
          shell: uninstall that thing

To something where I don't use block? either by with rescue (if it is possible to use rescue without block?) or by another means. Something like:

- name: my play
  hosts: 127.0.0.0
  any_errors_fatal: true
  connection: local
  tasks:
    - name: install something
      shell: install something
    - name: configure that something
      shell: do some things
      rescue:
        - name: rollback
          shell: uninstall that thing

?

Upvotes: 0

Views: 1019

Answers (1)

gary lopez
gary lopez

Reputation: 1954

There isn't way to do that. Rescue is exclusive of block, you could save a variable with register in your first task and execute other task with when if the variable was false but I guess it's better use block instead this way.

Upvotes: 2

Related Questions