Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Created June 4, 2025 06:42
Show Gist options
  • Save zeitounator/b006e340d5f073dfc1ee8c726ff9eba1 to your computer and use it in GitHub Desktop.
Save zeitounator/b006e340d5f073dfc1ee8c726ff9eba1 to your computer and use it in GitHub Desktop.
$ tree
.
├── roles
│   └── test_role
│   └── tasks
│   ├── after_fail.yml
│   ├── before_fail.yml
│   ├── induce_fail.yml
│   └── main.yml
└── test_import_role_and_block.yml
3 directories, 5 files
$ for f in roles/test_role/tasks/*; do echo -e "\n\n$f"; cat $f; done
roles/test_role/tasks/after_fail.yml
---
- name: after fail task
ansible.builtin.debug:
msg: After fail
roles/test_role/tasks/before_fail.yml
---
- name: Before fail task
ansible.builtin.debug:
msg: Before fail
roles/test_role/tasks/induce_fail.yml
---
- name: Induce failure
ansible.builtin.command: /bin/false
roles/test_role/tasks/main.yml
---
- name: Test import in block error control
block:
- ansible.builtin.import_tasks: before_fail.yml
- ansible.builtin.import_tasks: induce_fail.yml
always:
- ansible.builtin.import_tasks: after_fail.yml
$ cat test_import_role_and_block.yml
---
- name: Test importing role with import tasks in block
hosts: localhost
gather_facts: false
tasks:
- name: Run the test
ansible.builtin.import_role:
name: test_role
$ ansible-playbook test_import_role_and_block.yml
PLAY [Test importing role with import tasks in block] ******************************************************************************************************************************************************
TASK [test_role : Before fail task] ************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Before fail"
}
TASK [test_role : Induce failure] **************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002290", "end": "2025-06-04 08:40:04.067009", "msg": "non-zero return code", "rc": 1, "start": "2025-06-04 08:40:04.064719", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [test_role : after fail task] *************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "After fail"
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment