failed_when condition is not working

Hi Team,

I was trying to add one condition, in which if it should failed on that particular point. So, entire playbook should stop. Below is the written playbook.

- hosts: all
gather_facts: yes

pre_tasks:
- name: Verification Step before starting the post-patch playbook
pause:
prompt: “You are starting post patch playbook on {{ansible_hostname}} \n To Continue, please enter ‘YES’”
register: result

roles:
- role: add_post_patch_motd
failed_when: result.user_input != “YES”
- role: qradar_install/post_install/python_setup

- import_playbook: patch_cleanup.yml
- import_playbook: iso.yml
- import_playbook: post_patch_setup.yml

Here, user needs to enter interactive input for the “result” variable and if entered result value is other than “YES” then role should not be called and should get failed. So, as you can see I have used failed_when condition and based on that result value, it should not call role or get failed and entire playbook should stopped running. But now, even with failed_when condition satisfy, the role (add_post_patch_motd) is getting called. Do let me know what I am missing here.

This will always execute the role THEN fail if the condition is true. You need a different construct that only executes the condition

when: result.user_input == “YES”

I also don’t know if you can use conditions when using roles: like this. It may only work when you use the include_role as a task.

  • tasks:
  • include_role:
    name: add_post_patch_motd
    when: result.user_input == “YES”

Walter