# Playbook for windows updates.

**URL:** <https://forum.ansible.com/t/playbook-for-windows-updates/25588>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [September 25, 2017, 12:52pm UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588 "2017-09-25T12:52:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Sunil\_Kumar](https://forum.ansible.com/u/Sunil_Kumar)\
**Post date:** [September 25, 2017, 12:52pm UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588/1 "2017-09-25T12:52:36Z")

</div>

Hi Guys,

I am very new to Ansible. We are trying to automate windows montly patches. But our playbook keep on fail when there is a update which requires a reboot. How to handle intellegent system reboot only if it requires.

I have a very big doubt that once reboot is triggered the winrm connection will lost or it will wait for some seconds. If the connection is lost then we are planning to handle that with pause option but i need code logic it should pause the execution process only if rebooted is done.

Please help me.

Thanks,  
Sunil.

---

<div class="post-metadata">

**Author:** ![Mike\_Klebolt](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mike\_Klebolt](https://forum.ansible.com/u/Mike_Klebolt)\
**Post date:** [September 25, 2017, 6:30pm UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588/2 "2017-09-25T18:30:48Z")

</div>

The win\_updates module will give a reboot\_required: True return value when one is required. With that information you can register the task and then have a follow up win\_reboot task when reboot\_required: True. See below for how to accomplish this.

- name: Install Windows Updates  
win\_updates:  
register: updates

- name: Reboot after installing updates  
win\_reboot:  
when: updates.reboot\_required

---

<div class="post-metadata">

**Author:** ![Sunil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Sunil\_Kumar](https://forum.ansible.com/u/Sunil_Kumar)\
**Post date:** [September 27, 2017, 5:37am UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588/3 "2017-09-27T05:37:58Z")

</div>

Hey Mike,

What you given is absolutely correct. But i was blocked with different condition. While patching some updates won’t move forward without restart. in that case if i restart the connection established with ansible server will lost. For that i am using pause module in ansible. Once that pause is condition is executed how to resume the patching?

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [September 27, 2017, 11:15am UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588/4 "2017-09-27T11:15:36Z")

</div>

Hey Sunil

You should be using the win\_reboot module to handle updates. It reboots the server and waits for it to come back online so it should be seemless and won’t continue until it is ready for another task. Your playbook would look something like this

`

- name: install updates  
hosts: windows  
tasks:

- win\_updates:  
categories: CriticalUpdates  
state: installed  
register: update\_result

- win\_reboot:  
when: update\_result.reboot\_required

- name: second round of updates  
win\_updates:  
categories: CriticalUpdates  
state: installed  
register: update\_result

- win\_reboot:  
when: update\_result.reboot\_required  
`

Thanks

---

<div class="post-metadata">

**Author:** ![Sunil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Sunil\_Kumar](https://forum.ansible.com/u/Sunil_Kumar)\
**Post date:** [October 4, 2017, 10:41am UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588/5 "2017-10-04T10:41:56Z")

</div>

Hi Jordon,

I am almost there. Your code is working absolutely fine. But need to handle below logic.

1. We can’t say how many updates will ask for reboot so having win\_updates multiple times in playbook can’t work (we cannot hard code) → If we have the logic when ever reboot is triggered the control should go for win\_updates.

2. With the above given code. Second round of updates should execute only if win\_reboot is executed other wise it should skip.

---

<div class="post-metadata">

**Author:** ![Sivakumar\_Lakshminar](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Sivakumar\_Lakshminar](https://forum.ansible.com/u/Sivakumar_Lakshminar)\
**Post date:** [October 5, 2017, 3:31pm UTC](https://forum.ansible.com/t/playbook-for-windows-updates/25588/6 "2017-10-05T15:31:51Z")

</div>

Me too new just from my scenario I am suggesting to use a handler on play book level.  
And then added a notify to handler on each task where I expect a reboot to be done.

Below is a trial run we did.

.  
├── README.md  
├── ansible.cfg  
├── group\_vars  
│ ├── all  
│ ├── ccenter.yml  
│ ├── linux.yml  
│ └── windows.yml  
├── handlers  
│ └── main.yml  
├── hosts  
├── roles  
│ ├── oc  
│ │ └── tasks  
│ │ └── main.yml  
│ ├── todo  
│ │ ├── files  
│ │ │ └── temp\_files  
│ │ │ ├── 1.txt  
│ │ │ ├── 2.txt  
│ │ │ └── dummy  
│ │ │ └── dummy.txt  
│ │ └── tasks  
│ │ └── main.yml  
│ └── trial  
│ ├── files  
│ │ └── sample.xml  
│ ├── meta  
│ │ └── main.yml  
│ └── tasks  
│ ├── RedHat.yml  
│ ├── Windows.yml  
│ └── main.yml  
├── site.retry  
└── site.yml

**handler/main.yml**

- name: Reboot windows server

win\_reboot:  
msg: Reboot initiated by Ansible  
test\_command: whoami  
register: win\_reboot\_result

**sample task in the playbook**

- name: Install Web Deploy 3.6  
win\_package:  
name: Microsoft Web Deploy 3.6  
path: [http://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy\_amd64\_en-US.msi](http://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy_amd64_en-US.msi)  
product\_id: ‘{6773A61D-755B-4F74-95CC-97920E45E696}’  
arguments: ‘/q /norestart’  
state: present  
expected\_return\_code: [0, 666, 3010]  
notify:

- Reboot windows server

- name: force all notified handlers to run at this point, not waiting for normal sync points  
meta: flush\_handlers

Note: Reboot will be done if any change identified on that task.

Thanks,  
Sivakumar
