Riccardo79
Riccardo79

Reputation: 1056

Run last playbook even if there was an error previously

my test.yaml is something like:

---
- name: "PLB1"
  hosts: localhost
  gather_facts: no
  tasks:
    - debug: 
       msg: "1"

- name: "PLB2"
  hosts: localhost
  gather_facts: no
  tasks:
    - fail:
       msg: "oh no"
      when: 1==1       

- name: "PLB3"
  hosts: localhost
  gather_facts: no
  tasks:
    - debug: 
       msg: "2"

Is it possible to run PLB3 even if there was an error on PLB2? (ansible 4.8.0)

Riccardo

Upvotes: 0

Views: 230

Answers (1)

U880D
U880D

Reputation: 12121

The fail_module is for failing the run on condition. Nevertheless it would be possilbe to use failed_when: false with it.

---
- hosts: localhost
  gather_facts: no

  tasks:

  - name: PLB1
    debug:
      msg: "Task 1"

  - name: PLB2
    fail:
      msg: "Failed"
    when: "inventory_hostname == 'localhost'"
    failed_when: false

It would result in no message.

TASK [PLB2] ***
ok: [localhost]

If there should be an message you may use the assert_module.

  - name: PLB2
    assert:
      that:
        - "inventory_hostname != 'localhost'"
      fail_msg: "Failed"
      success_msg: "Succeeded"
    failed_when: false

  - name: PLB3
    debug:
      msg: "Task 3"

This would result into

TASK [PLB2] **********************************
ok: [localhost] => changed=false
  assertion: inventory_hostname != 'localhost'
  evaluated_to: false
  failed_when_result: false
  msg: Failed

TASK [PLB3] ******
ok: [localhost] =>
  msg: Task 3

Upvotes: 1

Related Questions