Hi everyone!
I’ve been using Ansible in production for a few years now to run all the scheduled tasks on the various Windows servers.
One of the routine tasks to be carried out is restarting the servers.
In my case, there are three situations:
- Restart the server unconditionally
- Restart if it needs to be restarted to apply updates, for example
- Force a restart if the uptime exceeds X days
In this article, I’m going to share these three playbooks.
Playbook 1: Restarting Windows
We’ll start with the ‘simplest’ playbook, which is to restart Windows; this playbook uses win_reboot.
---
- name: Windows Reboot
hosts: windows
tasks:
- name: Reboot
ansible.windows.win_reboot:
msg: Reboot by Ansible
reboot_timeout: 600
Upon restart, a message is displayed which will be available in the event observer. The task remains on hold for 600 seconds (10 minutes) whilst waiting for the server to come back online, which allows the successful execution of the playbook to be acknowledged.
Applying certain updates may take more than 10 minutes; in this case, the playbook will fail.
Playbook 2: Restart Windows if necessary
The second playbook will only restart Windows if necessary; to check this, we check whether a key exists in the Windows registry.
If this key exists, the server is restarted.
---
- name: Windows Reboot if needed
hosts: all
tasks:
- name: Check if reboot is required
ansible.windows.win_reg_stat:
path: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired
name: RebootRequiredSince
register: reboot_required
#- name: Debug Message
# ansible.builtin.debug:
# msg: Reboot is required for host
# when: reboot_required.exists == true
- name: Reboot
ansible.windows.win_reboot:
msg: Reboot by Ansible
reboot_timeout: 600
when: reboot_required.exists == true
Playbook 3: Restart Windows if the uptime exceeds X days
The latest playbook gave me a bit more of a headache, as there is no straightforward way to use Ansible to return the server’s uptime.
Here’s the playbook; I’ll explain it to you afterwards:
---
- name: Windows Reboot if needed
hosts: all
tasks:
- name: Check if reboot is required
ansible.windows.win_reg_stat:
path: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired
name: RebootRequiredSince
register: reboot_required
#- name: Debug Message
# ansible.builtin.debug:
# msg: Reboot is required for host
# when: reboot_required.exists == true
- name: Reboot
ansible.windows.win_reboot:
msg: Reboot by Ansible
reboot_timeout: 600
when: reboot_required.exists == true
The ‘toughest’ part was retrieving the Windows server’s uptime; to do this, I used PowerShell and ran it via win_shell .
To retrieve the uptime, I capture the console output so that I can use the result with Ansible; the variable containing the uptime is converted to JSON format.
Next, in Ansible, I process the output in JSON format and store it in a variable that I can use in the playbook.
From there, I can use the Windows server’s uptime to decide whether or not to restart it.
