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

35 views
Skip to first unread message

Ankit

unread,
Feb 25, 2019, 7:55:09 AM2/25/19
to Ansible Project
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

Kai Stian Olstad

unread,
Feb 25, 2019, 8:52:09 AM2/25/19
to ansible...@googlegroups.com
On 25.02.2019 13:55, Ankit wrote:
> 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.
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.


--
Kai Stian Olstad

Ankit

unread,
Mar 6, 2019, 2:59:02 AM3/6/19
to Ansible Project
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

Kai Stian Olstad

unread,
Mar 11, 2019, 10:05:44 AM3/11/19
to ansible...@googlegroups.com
On 06.03.2019 08:59, Ankit wrote:
> 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.

--
Kai Stian Olstad

Ankit Vashistha

unread,
Mar 11, 2019, 12:38:35 PM3/11/19
to ansible...@googlegroups.com
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.

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/130851ca-df0c-2656-e74e-849c2f6be5dd%40olstad.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages