Skip to content

Instantly share code, notes, and snippets.

@richm
Created November 13, 2020 17:57
Show Gist options
  • Save richm/f4c4cb13587b82652ef0284bf72bf8d4 to your computer and use it in GitHub Desktop.
Save richm/f4c4cb13587b82652ef0284bf72bf8d4 to your computer and use it in GitHub Desktop.
---
- name: test nested blocks
hosts: localhost
tasks:
- block:
- name: first outer level task
debug:
msg: first outer level task
- block:
- name: first inner level task
debug:
msg: first inner level task
- name: inner task that fails
fail:
msg: inner task that fails
- name: inner task that should never execute
debug:
msg: inner task that should never execute
rescue:
- name: inner block rescue task
debug:
msg: inner block rescue task ansible_failed_result is {{ ansible_failed_result | d("undefined") | to_nice_json }}
- name: re-raise error
fail:
msg: "{{ ansible_failed_result }}"
always:
- name: first inner block always task
debug:
msg: first inner block always task ansible_failed_result is {{ ansible_failed_result | d("undefined") | to_nice_json }}
- block:
- name: second block inner level task
debug:
msg: second block inner level task
- name: second outer level task
debug:
msg: second outer level task
rescue:
- name: outer block rescue task
debug:
msg: outer block rescue task ansible_failed_result is {{ ansible_failed_result | d("undefined") | to_nice_json }}
- name: check for error message
assert:
that: ansible_failed_result.msg == "inner task that fails"
This works:
TASK [inner task that fails] ***************************************************
task path: /home/rmeggins/ansible_sandbox/nested-block.yml:13
fatal: [localhost]: FAILED! => {
"changed": false
}
MSG:
inner task that fails
TASK [inner block rescue task] *************************************************
task path: /home/rmeggins/ansible_sandbox/nested-block.yml:20
ok: [localhost] => {}
MSG:
inner block rescue task ansible_failed_result is {
"_ansible_no_log": false,
"changed": false,
"failed": true,
"msg": "inner task that fails"
}
TASK [re-raise error] **********************************************************
task path: /home/rmeggins/ansible_sandbox/nested-block.yml:23
fatal: [localhost]: FAILED! => {
"changed": false
}
MSG:
{'failed': True, 'msg': 'inner task that fails', '_ansible_no_log': False, 'changed': False}
...
TASK [outer block rescue task] *************************************************
task path: /home/rmeggins/ansible_sandbox/nested-block.yml:38
ok: [localhost] => {}
MSG:
outer block rescue task ansible_failed_result is {
"_ansible_no_log": false,
"changed": false,
"failed": true,
"msg": "inner task that fails"
}
TASK [check for error message] *************************************************
task path: /home/rmeggins/ansible_sandbox/nested-block.yml:41
ok: [localhost] => {
"changed": false
}
MSG:
All assertions passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment