How to reboot remote Windows hosts with Ansible?
I’m going to show you a live Playbook with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible reboot Windows hosts
Today we’re talking about Ansible module win_reboot.
The full name is ansible.windows.win_reboot
which means is part of the ansible.windows collection
collection to target Windows host.
The purpose is to reboot a Windows machine, wait for it to go down, come back up, and respond to commands.
For non-Windows targets, use the ansible.builtin.reboot
module instead.
Please note that the connection user must have the SeRemoteShutdownPrivilege
Windows privilege enabled.
Parameters
- reboot_timeout float - 600
- msg string - “Reboot initiated by Ansible”
- connect_timeout float - 5
- pre_reboot_delay float - 2
- post_reboot_delay float - 0
- test_command string - “detect Windows Logon”
- boot_time_command string -
(Get-CimInstance -ClassName Win32_OperatingSystem -Property LastBootUpTime).LastBootUpTime.ToFileTime()
This module has not required parameters but some of them might are nice to know.
Let me summarize the most useful parameters.
The “reboot_timeout” defines how much time to expect before a machine returns up & running. The real timeout is double because of the process of reboot and test command success.
The connection to the target host is performed by WinRM TCP connection, with a timeout default to 5 seconds, which you could customize using the “connect_timeout” parameter.
The first step in the reboot process is to print a message to all the logged users. You could keep the default “Reboot initiated by Ansible” or customize using the “msg” parameter.
Secondly is going to execute the Windows reboot command.
You could define also some extra delay time using the “pre_reboot_delay” or “post_reboot_delay” integer. The first is default to two and the second to zero seconds.
Once rebooted the target host Ansible is going to verify the workstation fully working using a test command. The default script detects when the Windows Logon screen is up and ready to accept credentials, but you could customize using the “test_command” parameter.
An example about a specific service: exit (Get-Service -Name Netlogon).Status -ne "Running"'
This module could return also the amount of time indeed for bootstrap process reading thow Windows system, specifically (Get-CimInstance -ClassName Win32_OperatingSystem -Property LastBootUpTime).LastBootUpTime.ToFileTime()
.
Playbook
Let’s jump into a real-life playbook on how to reboot a Windows remote host with Ansible Playbook.
code
- win_reboot.yml
---
- name: win_reboot module Playbook
hosts: all
become: false
gather_facts: false
tasks:
- name: reboot host(s)
ansible.windows.win_reboot:
msg: "reboot by Ansible"
pre_reboot_delay: 5
post_reboot_delay: 10
execution
output
$ ansible-playbook -i win/inventory troubleshooting/incompatiblesudo_fix.yml
PLAY [win_reboot module Playbook] *********************************************************************
TASK [reboot host(s)] *****************************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Conclusion
Now you know how to reboot Windows hosts with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate