How to always run some ansible roles after previous failures?

2.7k Views Asked by At

I have a set of playbooks that do look like

- name: Run test
  hosts: tester
  roles:
    - { role: setup_repos }
    - { role: setup_environment }
    - { role: install_packages }
    - { role: run_tests }
    - { role: collect_logs }

The current problem is that all over the first 4 roles we have ignore_errors: true which is not a good practice as it makes very hard to read the output and to debug.

The only reason why ignore_errors was abused was because we wanted to be able to perform the collect_logs at the end, regardless the outcome.

How can we refactor this in order to remove the ignore_errors and have a more of a fail-fast strategy.

Please note that we have lots of playbooks calling collect_logs role, so "moving code inside playbook" is not really a way to reuse it.

2

There are 2 best solutions below

3
On BEST ANSWER

On ansible 2.4 or newer one should replace role: block with tasks like role_include or role_import, which gives you the ability to use normal logic used by tasks. You could use handlers, ignore_errors, and so on.

0
On

I belive handlers and notify would help you achive what you want. You wont need to change your roles behavior (although it might be a good idea).

You should notify at the end of each of your roles, the handler would only run once.

http://docs.ansible.com/ansible/latest/playbooks_intro.html#handlers-running-operations-on-change

Also if you choose to start handleing errors you could use the --force-handlers to force handler execution even when the playbook failed.

http://docs.ansible.com/ansible/latest/playbooks_error_handling.html#handlers-and-failure