- 1 year ago
Windows update by Ansible
To perform Windows updates using Ansible, you can utilize the win_updates
module. This module allows you to manage Windows updates and install available patches on Windows systems. Here's an example playbook to perform Windows updates using Ansible:
yaml---
- name: Perform Windows updates
hosts: windows_hosts
gather_facts: false
tasks:
- name: Check for available updates
win_updates:
category_names:
- CriticalUpdates
- SecurityUpdates
- UpdateRollups
- Updates
state: searched
register: updates_result
- name: Install available updates
win_updates:
category_names:
- CriticalUpdates
- SecurityUpdates
- UpdateRollups
- Updates
state: installed
when: updates_result.updates | length > 0
In this example:
Replace
windows_hosts
with the name or group of your target Windows hosts in your inventory file.The first task,
Check for available updates
, uses thewin_updates
module to search for available updates. It specifies the desired update categories using thecategory_names
parameter.The
state
parameter is set tosearched
to check for available updates without installing them. The result is stored in theupdates_result
variable.The second task,
Install available updates
, also uses thewin_updates
module. It specifies the same update categories and sets thestate
parameter toinstalled
to install the available updates.The task is conditional (
when: updates_result.updates | length > 0
) to ensure that updates are installed only if there are updates available.
You can save the playbook in a YAML file, such as windows_updates.yaml
, and execute it using the ansible-playbook
command:
shellansible-playbook -i inventory.ini windows_updates.yaml
Ensure that you have proper connectivity and authentication set up between Ansible and your Windows hosts. Additionally, ensure that you have the necessary permissions to perform Windows updates on the target hosts.
Make sure to review and test the playbook in your specific environment before executing it in a production environment.