# How to loop thru win\_updates until no updates left

**URL:** <https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [January 2, 2017, 3:39pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356 "2017-01-02T15:39:48Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 2, 2017, 3:39pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/1 "2017-01-02T15:39:48Z")

</div>

Hi,

I’m using win\_updates module to carry out windows patching and it works pretty good.  
Sometime, updates have dependencies and multiple playbook execution required.

I was thinking, if possible, to loop the playbook until “found\_update\_count \> 0” without the need of running ansible-playbook multiple time after each finish.

My playbook looks like this:

`

- hosts: win\_server\_1  
ignore\_errors: true  
gather\_facts: true

tasks:

- name: search wu  
win\_updates:  
category\_names:

- UpdateRollups

- CriticalUpdates

- SecurityUpdates  
state:

- searched  
log\_path:

- c:/temp/ansible\_wu.txt  
register: searched

- name: install wu  
win\_updates:  
category\_names:

- UpdateRollups

- CriticalUpdates

- SecurityUpdates  
state:

- installed  
log\_path:

- c:/temp/ansible\_wu.txt  
register: installed  
when: searched.found\_update\_count \> 0

- name: reboot  
win\_reboot:  
pre\_reboot\_delay\_sec: 0  
test\_command: whoami  
reboot\_timeout\_sec: 300  
when: installed.reboot\_required = ‘true’  
register: reboot

`

I cant figure out how to put this in playbook by myself, seeking for some guidance.

Thanks,

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [January 2, 2017, 7:28pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/2 "2017-01-02T19:28:00Z")

</div>

I can’t from the top of my head recall if the “block” feature supports loops, but if it does I guess that’s the best way to do this.

As far as I can remember, Microsoft’s own config management tool for clients (SCCM) gets around this by simply doing 2 “passes” of patching. You could do that aswell, with some conditionals to only kick the second pass if it’s needed.

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 3, 2017, 12:18pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/3 "2017-01-03T12:18:39Z")

</div>

Hi,

Thank for the heads up but it seems like Block does not support ‘until’

`  
ERROR! ‘until’ is not a valid attribute for a Block

`

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 3, 2017, 12:56pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/4 "2017-01-03T12:56:04Z")

</div>

I’ve also read a post by Brian Coca which stated that “ **blocks do not support any type of loop** ”.  
What other options can be used to re-run multiple tasks (one that checks/installs updates and the other reboots the server) until no updates available?

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 4, 2017, 6:43pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/5 "2017-01-04T18:43:51Z")

</div>

Anyone? ☹

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [January 8, 2017, 8:01pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/6 "2017-01-08T20:01:24Z")

</div>

Never tried it myself, but wouldn't this work ?

- include: update-windows.yml  
&nbsp;&nbsp;&nbsp;until: \<condition\>  
&nbsp;&nbsp;&nbsp;retries: 10

You will have to make sure that the condition is based on facts from the tasks in your taskbook, which include win\_updates and win\_reboot.

The only other option I see is to add win\_reboot capabilities to win\_updates, which would be a shame if that's what is needed.

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 8, 2017, 9:37pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/7 "2017-01-08T21:37:20Z")

</div>

I actually tried this and it didn’t work.

Later, found this post [https://groups.google.com/forum/#!topic/ansible-project/xGGe6WADtH0](https://groups.google.com/forum/#!topic/ansible-project/xGGe6WADtH0)

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 9, 2017, 1:44pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/8 "2017-01-09T13:44:50Z")

</div>

I’ve found a working solution, it’s ugly and probably will cause more headache but it works.

My solution looks like this:

`

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [January 11, 2017, 2:26pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/9 "2017-01-11T14:26:01Z")

</div>

Nice trick, thank you for sharing this.

Jon

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [January 12, 2017, 2:02am UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/10 "2017-01-12T02:02:26Z")

</div>

The solution Dag posted is what I’ve always done, and it works great for me. I’ve been advocating for block loop support (as a cleaner solution to exactly this issue) since before it shipped, but I don’t have the bandwidth to implement myself right now, and around here it’s kinda “put up or shut up”. 😉 If it doesn’t work for you, let us know why and maybe we can get it figured out.

I really wouldn’t recommend the “run the playbook in a loop” thing- you lose a lot of output fidelity and error handling, and it’s really just a way more expensive way to do what Dag suggested.

I actually originally wrote win\_updates with a wrapper action that would handle the reboots automatically, but for various reasons (that I can’t recall) decided to abandon the wrapper before I shipped it…

-Matt

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 12, 2017, 9:27am UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/11 "2017-01-12T09:27:20Z")

</div>

Hi Matt,

Dag’s solution was something I really hoped to work but then I found this post by Brian Coca - [https://groups.google.com/forum/#!topic/ansible-project/xGGe6WADtH0](https://groups.google.com/forum/#!topic/ansible-project/xGGe6WADtH0)

Seems like this is not possible because “Include is not a module, more like a preprocessing macro.”

---

<div class="post-metadata">

**Author:** ![Jinesh\_Choksi](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jinesh\_Choksi](https://forum.ansible.com/u/Jinesh_Choksi)\
**Post date:** [January 12, 2017, 5:07pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/12 "2017-01-12T17:07:51Z")

</div>

Hi Danny,

Did you come across this post - [https://groups.google.com/d/msg/ansible-project/E1ETAVoG6d0/6vC0PGCpAwAJ](https://groups.google.com/d/msg/ansible-project/E1ETAVoG6d0/6vC0PGCpAwAJ) ?

> Right now looping over includes is doable by doing a **with\_** statement. What I trying to achieve right now would look like:

> - include: run-in-loop.yml

> with\_sequence: start=0 end=\<+INFINITE\>  
> loop\_control:  
> loop\_var: main\_item

You would need to break execution in the included playbook if you detect that updates are done using - [http://stackoverflow.com/a/22760829](http://stackoverflow.com/a/22760829)

Regards,  
Jinesh

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [January 12, 2017, 10:26pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/13 "2017-01-12T22:26:29Z")

</div>

That post is really old- include looping has worked for a long time…

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 13, 2017, 10:54am UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/14 "2017-01-13T10:54:38Z")

</div>

Then I cannot figure out what am I doing wrong, can you please assist?

main.yml:

`

---

<div class="post-metadata">

**Author:** ![Jinesh\_Choksi](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jinesh\_Choksi](https://forum.ansible.com/u/Jinesh_Choksi)\
**Post date:** [January 13, 2017, 2:14pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/15 "2017-01-13T14:14:57Z")

</div>

- “until: is not currently supported on includes.” ([https://github.com/ansible/ansible/issues/17098](https://github.com/ansible/ansible/issues/17098))

- “When you call include, Ansible actually places tasks from included file into the execution queue after the current task.” ([http://stackoverflow.com/a/38481496](http://stackoverflow.com/a/38481496))

The example below is a hack but it has a useful property whereby tasks in the inner taskbook will not be run if the play was ended. i.e.

calling inner-taskbook 1st time

- running tasks

calling inner-taskbook 2nd time

- running tasks

calling inner-taskbook 3rd time

- running tasks
- play ended because we’ve determined that we’ve finished the work

calling inner-taskbook 4th time

- noop

calling inner-taskbook 5th time

- noop

calling inner-taskbook nth time

- noop

So if you configure the with\_sequence parameters as you would have done the “until:” command’s “retries” attribute then you kinda simulate the retry functionality with the example below.

You will notice that the taskbook does get “included” 100 times but the tasks in it are ONLY executed the number of times actually required.

run.sh:  
—snip—  
#!/usr/bin/env bash

ansible-playbook -vvv -i ‘localhost,’ -c local main.yml  
—snip—

main.yml:

`

---

<div class="post-metadata">

**Author:** ![Jinesh\_Choksi](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jinesh\_Choksi](https://forum.ansible.com/u/Jinesh_Choksi)\
**Post date:** [January 13, 2017, 2:41pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/16 "2017-01-13T14:41:49Z")

</div>

Ack. I just noticed that the example below is not a good solution since it never did run the “Performing tasks after windows updates.” task in the main.yml file.

Sorry. Please ignore the example.

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [January 19, 2017, 8:00pm UTC](https://forum.ansible.com/t/how-to-loop-thru-win-updates-until-no-updates-left/23356/17 "2017-01-19T20:00:05Z")

</div>

Bummer, this was the best solution so far!
