Patching by hand doesn’t scale past a few dozen servers, and it’s exactly the kind of repetitive, high-risk work automation is for. This guide builds a patching playbook that checks first, patches in safe batches, and stops if things go wrong. Read Patching Windows Server safely first for the process behind it.
The playbook
playbooks/patch-windows.yml:
- name: Patch Windows servers in rolling batches
hosts: "{{ target | default('patch_wave1') }}"
gather_facts: false
serial: "{{ batch | default('25%') }}"
max_fail_percentage: 0
tasks:
- name: Pre-check - free space on C (GB)
ansible.windows.win_shell: |
[math]::Round((Get-PSDrive C).Free / 1GB, 1)
register: c_free
changed_when: false
check_mode: false # read-only, so also run in --check mode
- name: Stop if C has less than 10 GB free
ansible.builtin.assert:
that: c_free.stdout | float >= 10
fail_msg: "{{ inventory_hostname }} has only {{ c_free.stdout | trim }} GB free on C"
- name: Record running automatic services
ansible.windows.win_shell: |
Get-CimInstance Win32_Service -Filter "StartMode='Auto' AND State='Running'" |
Select-Object -ExpandProperty Name
register: services_before
changed_when: false
check_mode: false # read-only, so also run in --check mode
- name: Install security and critical updates
ansible.windows.win_updates:
category_names:
- SecurityUpdates
- CriticalUpdates
reboot: true
reboot_timeout: 3600
register: update_result
- name: Check automatic services again
ansible.windows.win_shell: |
Get-CimInstance Win32_Service -Filter "StartMode='Auto' AND State='Running'" |
Select-Object -ExpandProperty Name
register: services_after
changed_when: false
check_mode: false # read-only, so also run in --check mode
- name: Services that stopped after patching
ansible.builtin.set_fact:
stopped_services: "{{ services_before.stdout_lines | difference(services_after.stdout_lines) }}"
- name: Fail if a service did not come back
ansible.builtin.assert:
that: stopped_services | length == 0
fail_msg: "{{ inventory_hostname }}: not running after patching: {{ stopped_services | join(', ') }}"
- name: Summary
ansible.builtin.debug:
msg: "{{ inventory_hostname }}: installed {{ update_result.installed_update_count }} update(s), reboot required afterwards: {{ update_result.reboot_required }}"
How the safety works
serial: 25%– patch a quarter of the target group at a time. Ansible finishes each batch (including reboots and checks) before starting the next.max_fail_percentage: 0– if any server in a batch fails, Ansible stops before touching the next batch. One bad update hits a few servers, not all of them.- Pre-checks – low disk space stops that server before anything is installed.
- Post-checks – a service that was running before and isn’t afterwards fails the host, so a human looks at it.
check_mode: falseon the read-only checks lets--checkrun them for real, so a dry run shows accurate results.reboot: true–win_updatesreboots as needed and continues until the updates are installed, up toreboot_timeout.
Plan waves in the inventory
all:
children:
patch_wave1: # test and low-risk servers – week 1
hosts: { APP-TEST01: {}, WEB-TEST01: {} }
patch_wave2: # first half of production – week 2
hosts: { WEB01: {}, APP01: {} }
patch_wave3: # second half, incl. cluster partners of wave 2
hosts: { WEB02: {}, APP02: {} }
Put the two nodes of a pair or cluster in different waves (or set serial: 1 for them) so a service never loses both nodes at once.
Run it
# 1. See exactly which servers are in scope
ansible-playbook playbooks/patch-windows.yml -e target=patch_wave1 --list-hosts
# 2. Search only – list available updates without installing
ansible-playbook playbooks/patch-windows.yml -e target=patch_wave1 --check
# 3. Patch
ansible-playbook playbooks/patch-windows.yml -e target=patch_wave1 -e batch=2
Wrap it in a Jenkins pipeline with a plan → approve → apply gate (see the shared library guide) and you have auditable, repeatable patching.
Other useful options
state: searched– only list updates (great for a compliance report).accept_list/reject_list– include or exclude specific KBs (e.g. hold back one known-bad update).log_path– keep the Windows Update log on the server for troubleshooting.
Check the ansible.windows.win_updates documentation for your collection version – option names have changed between releases.
FAQ
What about application owners’ checks? Automation proves the server is healthy; only the app owner can confirm the app works. Add a notification after each wave and wait for sign-off before the next wave.
What’s next
Next: Safe changes – check mode, idempotency and Ansible Vault.
Practise this topic
Test yourself with free questions on this topic – instant answers and explanations.
Start practice questionsAnsiblePatchingWindows UpdatesAutomationAdvanced