Ansible Pilot

Reboot Windows hosts - Ansible module win_reboot

Reboot Windows hosts

Ansible module win reboot

How to reboot remote Windows hosts with Ansible? I’m going to show you a live demo 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

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().

demo

Let’s jump into a real-life playbook on how to reboot a Windows remote host with Ansible Playbook.

---
- name: win_reboot module demo
  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

output

$ ansible-playbook -i win/inventory troubleshooting/incompatiblesudo_fix.yml
PLAY [win_reboot module demo] *********************************************************************
TASK [reboot host(s)] *****************************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

code with ❤️ in GitHub

Recap

Now you know how to reboot Windows hosts with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Donate

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

September 27, 2021