Skip to content

Commit

Permalink
Avoid using ansible_failed_task in rescue blocks
Browse files Browse the repository at this point in the history
ansible_failed_task can contain unexpanded Jinja2 templates with
variable references that were valid when the task failed, but not valid
anymore when using the variable in the rescue block. In this case the
problem is with task names (.name) that contain references to
storage_provider. Unfortunately this completely prevents using
ansible_failed_task even if not using the problematic attribute. Other
attribute could have been used if this were not the case, like .tags,
but until ansible/ansible#49942 is fixed, it
is better to avoid using ansible_failed_task and use the only other
special variable describing the failed task: ansible_failed_result.

Eliminate the previous workaround to set storage_provider on the tasks
that expand ansible_failed_task and use ansible_failed_result with a
special value of msg instead. This has a disadvantage of having to use a
special msg with the fail module, while usually it is just a free-form
message. The fail module does not set another attribute of the result
that could be used though, and it is cleaner than the previous
workaround.
  • Loading branch information
pcahyna committed May 7, 2020
1 parent 10a78d7 commit 787f00c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 84 deletions.
45 changes: 15 additions & 30 deletions tests/tests_disk_errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@
disks: ['/dev/surelyidonotexist']
mount_point: "{{ mount_location }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly,
# blivet_output.failed is false.
Expand Down Expand Up @@ -114,19 +111,16 @@
fs_type: 'ext3'
disks: "{{ unused_disks }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand Down Expand Up @@ -158,19 +152,16 @@
fs_type: 'ext3'
disks: "{{ unused_disks }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand Down Expand Up @@ -212,19 +203,16 @@
disks: "{{ unused_disks }}"
type: partition

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand All @@ -244,19 +232,16 @@
disks: "{{ unused_disks }}"
type: lvm

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand Down
81 changes: 27 additions & 54 deletions tests/tests_lvm_errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,16 @@
size: "{{ volume1_size }}"
mount_point: "{{ mount_location1 }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly
# - debug:
Expand Down Expand Up @@ -78,19 +75,16 @@
size: "{{ invalid_size }}"
mount_point: "{{ mount_location1 }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly
# - name: Verify the output
Expand All @@ -114,19 +108,16 @@
size: "{{ too_large_size }}"
mount_point: "{{ mount_location1 }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly
# - name: Verify the output
Expand All @@ -150,19 +141,16 @@
size: "{{ too_large_size }}"
mount_point: "{{ mount_location1 }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly
# - name: Verify the output
Expand All @@ -186,19 +174,16 @@
size: "{{ too_large_size }}"
mount_point: "{{ mount_location1 }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly
# - name: Verify the output
Expand All @@ -221,19 +206,16 @@
size: "{{ volume1_size }}"
mount_point: "{{ mount_location1 }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

# the following does not work properly
# - name: Verify the output
Expand Down Expand Up @@ -347,19 +329,16 @@
fs_type: 'ext3'
size: '1g'

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand Down Expand Up @@ -401,19 +380,16 @@
disks: "{{ unused_disks }}"
type: lvm

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand All @@ -435,19 +411,16 @@
disks:
- "{{ unused_disks[0] }}"

- name: UNREACH
- name: unreachable task
fail:
msg: "this should be unreachable"
msg: UNREACH

rescue:
- name: Check that we failed in the role
assert:
that:
- ansible_failed_task.name != 'UNREACH'
- ansible_failed_result.msg != 'UNREACH'
msg: "Role has not failed when it should have"
vars:
# Ugh! needed to expand ansible_failed_task
storage_provider: blivet

- name: Verify the output
assert:
Expand Down

0 comments on commit 787f00c

Please sign in to comment.