|
| 1 | +--- |
| 2 | +# Task file: save facts, clear_facts, run linux-system-roles.ha_cluster, then restore facts. |
| 3 | +# Include this with include_tasks or import_tasks; ensure tests/library is in module search path. |
| 4 | +# Input: |
| 5 | +# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role |
| 6 | +# - __sr_public: export private vars from role - same as public in include_role |
| 7 | +# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role |
| 8 | +# Output: |
| 9 | +# - ansible_facts: merged saved ansible_facts with ansible_facts modified by the role, if any |
| 10 | +- name: Clear facts |
| 11 | + meta: clear_facts |
| 12 | + |
| 13 | +# note that you can use failed_when with import_role but not with include_role |
| 14 | +# so this simulates the __sr_failed_when false case |
| 15 | +# Q: Why do we need a separate task to run the role normally? Why not just |
| 16 | +# run the role in the block and rethrow the error in the rescue block? |
| 17 | +# A: Because you cannot rethrow the error in exactly the same way as the role does. |
| 18 | +# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort. |
| 19 | +- name: Run the role with __sr_failed_when false |
| 20 | + when: |
| 21 | + - __sr_failed_when is defined |
| 22 | + - not __sr_failed_when |
| 23 | + block: |
| 24 | + - name: Run the role |
| 25 | + include_role: |
| 26 | + name: linux-system-roles.ha_cluster |
| 27 | + tasks_from: "{{ __sr_tasks_from | default('main') }}" |
| 28 | + public: "{{ __sr_public | default(false) }}" |
| 29 | + rescue: |
| 30 | + - name: Ignore the failure when __sr_failed_when is false |
| 31 | + debug: |
| 32 | + msg: Ignoring failure when __sr_failed_when is false |
| 33 | + |
| 34 | +- name: Run the role normally |
| 35 | + include_role: |
| 36 | + name: linux-system-roles.ha_cluster |
| 37 | + tasks_from: "{{ __sr_tasks_from | default('main') }}" |
| 38 | + public: "{{ __sr_public | default(false) }}" |
| 39 | + when: __sr_failed_when | d(true) |
0 commit comments