# How can i run a block of playbook instructions only if they pass the specified condition?

**URL:** <https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761>\
**Category:** Ansible Project\
**Created:** [February 25, 2019, 12:55pm UTC](https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761 "2019-02-25T12:55:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [February 25, 2019, 12:55pm UTC](https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761/1 "2019-02-25T12:55:09Z")

</div>

I have got a playbook where i need to check if the host fact for reboot is set to true, i need to execute the rest of the instructions for that particular host in the inventory only if the reboot condition is true.  
Following is the playbook. I would like to avoid executing all the tasks for the host where the condition **ansible\_facts[‘Pending\_Reboot’] == false** or **when: ansible\_facts[‘Pending\_Reboot’] != false.** In such case, the execution should be skipped for that particular host and move to next host in the inventory.

`

- hosts: all  
gather\_facts: false  
tasks:

- name: Check and Reboot Server based on Pending\_Reboot Fact  
shell: “sleep 5 && reboot”  
async: 1  
poll: 0  
when: ansible\_facts[‘Pending\_Reboot’] == true

- name: Pause for 10 seconds  
pause:  
seconds: 10

- name: Wait for Re-connection  
wait\_for\_connection:  
connect\_timeout: 5  
sleep: 5  
delay: 90  
timeout: 600

- name: Run checkLinuxSystemUpdateStatusTask task file to re-validate Reboot Status  
include: checkLinuxSystemUpdateStatusTask.yml  
`

What is the best and least time consuming way of doing this? I was checking the documentation for **block** but i am not sure if i can use the conditions for a block.

Thanks in Advance.

**Regards,**  
**Ankit**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 25, 2019, 1:52pm UTC](https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761/2 "2019-02-25T13:52:01Z")

</div>

Why not just test it?  
or check the documentation for what block support?  
[https://docs.ansible.com/ansible/latest/reference\_appendices/playbooks\_keywords.html#block](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#block)

BTW when is the list.

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [March 6, 2019, 7:59am UTC](https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761/3 "2019-03-06T07:59:02Z")

</div>

Ok, i tried this but **block** doesn’t accept, **retries.** I have the following playbook currently. Here, until works but i am not able to figure out a way to limit the max number of iterations:

> `
> 
> - hosts: all
> 
> name: runLinuxSystemUpdate
> 
> gather\_facts: false
> 
> tasks:
> 
> - name: Set Facts if not present in Host.
> 
> include: checkLinuxSystemUpdateStatusTask.yml
> 
> when: ansible\_facts[‘Missing\_Hotfix\_Patches’] is not defined or ansible\_facts[‘Missing\_Hotfix\_Patches’] == “”
> 
> - name: Iteration Block
> 
> block:
> 
> - name: Install Patches Block
> 
> block:
> 
> - name: Run InstallLinuxPatchesTask.yml Task File
> 
> include: InstallLinuxPatchesTask.yml
> 
> when: ansible\_facts[‘Missing\_Hotfix\_Patches’] != ‘0’ or ansible\_facts[‘Missing\_Security\_Patches’] != ‘0’
> 
> - name: Reboot Machines Block
> 
> block:
> 
> - name: Run RebootLinuxMachinesTask.yml Task File
> 
> include: RebootLinuxMachinesTask.yml
> 
> when: ansible\_facts[‘Pending\_Reboot’] == true
> 
> - name: Run checkLinuxSystemUpdateStatusTask task file to re-validate Update and Reboot Status
> 
> include: checkLinuxSystemUpdateStatusTask.yml
> 
> #when: update\_result.stdout.find(“The deployment of patches and packages was successfully”) == -1
> 
> until: ansible\_facts[‘Missing\_Hotfix\_Patches’] != ‘0’ and ansible\_facts[‘Missing\_Security\_Patches’] != ‘0’  
> **retries: 3**  
> `

**Regards,**  
**Ankit**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 7, 2019, 5:17pm UTC](https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761/4 "2019-03-07T17:17:14Z")

</div>

> Ok, i tried this but \*block\* doesn't accept, \*retries.

If you have looked at the documentation I linked to you would see that retries is not allowed for a block, only task.  
So you need to move your retries to the individual tasks.

> \*I have the  
> following playbook currently. Here, until works but i am not able to figure  
> out a way to limit the max number of iterations:
> 
> - hosts: all
> 
> &nbsp;&nbsp;&nbsp;name: runLinuxSystemUpdate
> 
> &nbsp;&nbsp;&nbsp;gather\_facts: false
> 
> &nbsp;&nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Set Facts if not present in Host.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include: checkLinuxSystemUpdateStatusTask.yml
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_facts['Missing\_Hotfix\_Patches'] is not defined or  
> ansible\_facts['Missing\_Hotfix\_Patches'] == ""
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Iteration Block
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Install Patches Block
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Run InstallLinuxPatchesTask.yml Task File
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include: InstallLinuxPatchesTask.yml
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_facts['Missing\_Hotfix\_Patches'] != '0' or  
> ansible\_facts['Missing\_Security\_Patches'] != '0'

When you not using rescue and/or always in block just having one task in a block has no meaning.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Reboot Machines Block
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Run RebootLinuxMachinesTask.yml Task File
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include: RebootLinuxMachinesTask.yml
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_facts['Pending\_Reboot'] == true

Same here, this block has no meaning since it only on one task.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Run checkLinuxSystemUpdateStatusTask task file to re-validate  
> Update and Reboot Status
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include: checkLinuxSystemUpdateStatusTask.yml
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#when: update\_result.stdout.find("The deployment of patches and  
> packages was successfully") == -1
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until: ansible\_facts['Missing\_Hotfix\_Patches'] != '0' and  
> ansible\_facts['Missing\_Security\_Patches'] != '0'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\*retries: 3\*

The retries you need to move to each task.

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [March 11, 2019, 4:38pm UTC](https://forum.ansible.com/t/how-can-i-run-a-block-of-playbook-instructions-only-if-they-pass-the-specified-condition/29761/5 "2019-03-11T16:38:08Z")

</div>

Thanks Kai.

I had all the tasks running with own retries but i was hoping to have retries condition for multiple tasks or a bunch of tasks having single condition and retries. It is strange that there is no way to get that done.
