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

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

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

BTW when is the list.

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

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

   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'

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

         - name: Reboot Machines Block

           block:

             - name: Run RebootLinuxMachinesTask.yml Task File

               include: RebootLinuxMachinesTask.yml

           when: ansible_facts['Pending_Reboot'] == true

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

         - 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*

The retries you need to move to each task.

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.