4

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.

sorin
  • 161,544
  • 178
  • 535
  • 806
  • How about creating a playbook that runs the `collect_logs` role only and run it after the first completes? – PhilipGough Aug 31 '17 at 14:39
  • This would be against a clean and simple user interface. It adds complexity to the user which is worst. – sorin Aug 31 '17 at 14:49
  • Drive your testing with a shell script that calls a standalone `collect_logs` playbook after everything else has completed, regardless of the exit status of previous ansible runs. This way you can drop your `ignore_errors` and correctly report failures while still collecting logs. – larsks Sep 04 '17 at 02:46

2 Answers2

1

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.

sorin
  • 161,544
  • 178
  • 535
  • 806
0

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

nadavbrkt
  • 87
  • 5