How to always run some ansible roles after previous failures?

坚强是说给别人听的谎言 提交于 2019-12-12 14:24:23

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/45983636/how-to-always-run-some-ansible-roles-after-previous-failures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!