6

Why am I not able to use changed_when to change the state of a task that return failed to changed?

For example, I have a custom module that allows me to check the existence of a volume, it will failed if the volume doesn't exist.

Playbook snippet:

---
- name: Example ansible playbook
  tasks:
    - name: Check existence of the volume
      custom_name_space_my_volume:
        name: ABC #name of the volume
      changed_when: true

If the volume ABC doesn't exist, the result is failed. But why didn't change_when: true causes the result to return changed instead?

Note: The example above might not make logical sense since it's not doing something useful. But I just wanted to use it as a sample for the question I have.

toydarian
  • 4,246
  • 5
  • 23
  • 35
caramel1995
  • 2,968
  • 10
  • 44
  • 57
  • There are additional discussions you may find interesting: https://stackoverflow.com/q/23946112/320399 , https://stackoverflow.com/q/31731756/320399 , and https://stackoverflow.com/q/62182998/320399 – blong Apr 30 '21 at 20:18

1 Answers1

4

Because failed overrides changed, when a task fails.

For example, take the shell-module. By default it will always show changed, as ansible can't decide if your script did anything or not. But it can also fail, if the script exits with an error-code that is not 0. When it does that, you want it to show failed and not changed.

To make sure, your task will never end up failed, you can add failed_when: false.

---
- name: Example ansible playbook
  tasks:
    - name: Check existence of the volume
      custom_name_space_my_volume:
        name: ABC #name of the volume
      changed_when: true
      failed_when: false

This will make your task to never show failed, so it would be better to provide a sensible expression to mark it as failed when it actually failed.

toydarian
  • 4,246
  • 5
  • 23
  • 35