--- # Install Windows Updates on the [windows] inventory group and reboot if required. # Usage: # ansible-playbook windows/update.yaml # install + reboot if needed # ansible-playbook windows/update.yaml -e reboot=false # install only, report if reboot pending # ansible-playbook windows/update.yaml -e search_only=true # just list available updates - name: Update Windows servers hosts: windows gather_facts: false vars: reboot: true search_only: false update_categories: - SecurityUpdates - CriticalUpdates - UpdateRollups - DefinitionUpdates - Updates tasks: - name: Check connectivity ansible.windows.win_ping: - name: Search for available updates ansible.windows.win_updates: category_names: "{{ update_categories }}" state: searched register: search when: search_only | bool - name: List available updates ansible.builtin.debug: msg: "{{ search.updates | dict2items | map(attribute='value.title') | list }}" when: search_only | bool - name: Install updates ansible.windows.win_updates: category_names: "{{ update_categories }}" state: installed reboot: "{{ reboot | bool }}" reboot_timeout: 3600 register: result when: not search_only | bool # With reboot=false, updates blocked by the pending restart (WU_E_INSTALL_NOT_ALLOWED) # are expected; only fail on other errors. failed_when: >- result.failed_update_count | default(0) | int > 0 and (reboot | bool or result.updates | dict2items | map(attribute='value') | selectattr('failure_hresult_code', 'defined') | rejectattr('failure_hresult_code', 'equalto', 2149842966) | list | length > 0) - name: Report ansible.builtin.debug: msg: - "Installed: {{ result.updates | dict2items | map(attribute='value') | selectattr('installed') | map(attribute='title') | list }}" - "Not installed: {{ result.updates | dict2items | map(attribute='value') | rejectattr('installed') | map(attribute='title') | list }}" - "Reboot still required: {{ result.reboot_required }}{{ ' - reboot and re-run to finish' if result.reboot_required else '' }}" when: not search_only | bool