failed_when condition is not working

30 views
Skip to first unread message

Neha Singh

unread,
Oct 19, 2022, 1:17:34 AM10/19/22
to Ansible Project
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. 

Rowe, Walter P. (Fed)

unread,
Oct 19, 2022, 7:34:20 AM10/19/22
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a58aa60c-053e-45df-be40-e3afe17cc1dan%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages