Avoid writing same condition to call role or playbook

15 views
Skip to first unread message

Neha Singh

unread,
Oct 12, 2022, 12:08:59 AM10/12/22
to Ansible Project
Hi Team,

I have started working on Ansible from last 2-3 months only, so not able to remember all module by heart. I did setup one env variable (result) and then based on that value I am trying to call the role and playbook is importing. Below is the code for the same

- hosts: all 
gather_facts: yes 

roles: 
 - role: add_post_patch_motd
   when: result.user_input == "YES"
 - role: qradar_install/post_install/python_setup
   when: result.user_input == "YES" 

- import_playbook: iso.yml
  when: result.user_input == "YES"
- import_playbook: post_patch_setup.yml
  when: result.user_input == "YES"
- import_playbook: post_patch_commands.yml
  when: result.user_input == "YES" 
- import_playbook: remove_old_tls.yml
  when: result.user_input == "YES"

Here I am using same when condition to call role and import_playbook, which is somewhat like a duplication of code. I got to know that blocks can be used via task, so cannot used here. Used looping too, but not able to figure out exact way. There is one failed_when parameter, I tried to use it with one role to call, so that if it gets failed, all other statement will get skip, but it doesn't worked for me. I wanted that for result value, other than "YES" it should skip any other statement in playbook.

Could you please help me how to use the same here

Rowe, Walter P. (Fed)

unread,
Oct 12, 2022, 7:49:42 AM10/12/22
to ansible...@googlegroups.com
https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html

Place your condition on the entire block.

block:
  - task1:
      ...
  - task2:
      ...
  - taskN:
      ...
when: condition

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/fcf22986-9c3c-413d-836e-5fe85fc56a65n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages