Learn › Ansible

Patch Windows Servers with Ansible: win_updates and Rolling Batches

Automate Windows patching with Ansible: pre-checks, win_updates with reboots, rolling batches with serial and failure limits, and a post-patch report.

Advanced 12 min read
By Gopinath Karthikeyan

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: false on the read-only checks lets --check run them for real, so a dry run shows accurate results.
  • reboot: true – win_updates reboots as needed and continues until the updates are installed, up to reboot_timeout.
Rolling patching with Ansible serial batches and a failure limit Batch 1 serial: 2 Batch 1 serial: 2 S1 S1 S2 S2 Health check OK → next batch Health check OK → அடுத்த batch Batch 2 serial: 5 Batch 2 serial: 5 S3 S3 S4 S4 S5 S5 S6 S6 S7 S7 Health check OK → next batch Health check OK → அடுத்த batch Batch 3 the rest Batch 3 மீதம் S8 S8 S9 S9 S10 S10 S11 S11 … … Too many failures → stop the rollout max_fail_percentage protects the remaining servers அதிக failures → rollout நிறுத்து max_fail_percentage மீதி servers-ஐ காக்கும்
Patch a small canary batch first, check health, then widen the batches. A failure limit stops the run before a bad update reaches every server.முதலில் சிறிய batch, health check, பிறகு பெரிய batches. Failure limit, தவறான update எல்லா servers-க்கும் போகாமல் நிறுத்தும்.

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 questions

AnsiblePatchingWindows UpdatesAutomationAdvanced

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in Ansible