# Avoid writing same condition to call role or playbook

**URL:** <https://forum.ansible.com/t/avoid-writing-same-condition-to-call-role-or-playbook/37208>\
**Category:** Ansible Project\
**Created:** [October 12, 2022, 4:08am UTC](https://forum.ansible.com/t/avoid-writing-same-condition-to-call-role-or-playbook/37208 "2022-10-12T04:08:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![neha0021997](https://avatars.discourse-cdn.com/v4/letter/n/34f0e0/32.png) [@neha0021997](https://forum.ansible.com/u/neha0021997)\
**Post date:** [October 12, 2022, 4:08am UTC](https://forum.ansible.com/t/avoid-writing-same-condition-to-call-role-or-playbook/37208/1 "2022-10-12T04:08:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [October 12, 2022, 11:49am UTC](https://forum.ansible.com/t/avoid-writing-same-condition-to-call-role-or-playbook/37208/2 "2022-10-12T11:49:15Z")

</div>

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_blocks.html](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
