Automate Remote Windows Host Services Using Ansible

Sonjesaurabh
Globant
Published in
4 min readFeb 1, 2021

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, infra-service orchestration and many other requirements.

Introduction

This article is all about managing remote Windows host services using Ansible. To bring good practise at work, rather than doing manual activity we can create automation using Ansible. This solves a lot of issues like managing and executing core functions in Windows environments. There are multiple tools available to do this, but here we are using Ansible because it provides a service module for automation of services.

Let’s begin with the setup.

Prerequisites:

1. Centos 8 OS installed with Ansible.

2. Windows 10 Remote Host.

Installation Steps:

Following steps should be run on Centos:

Step 1: Install Python3 on Ansible control node.

sudo dnf install python3

Step 2: Verify if python3 is installed.

python3 --version

Step 3: Install a virtual environment to run Ansible .

sudo dnf install python3-virtualenv

step 4: Create a virtual workspace environment

virtualenv env

Step 5: Activate virtual environment

source env/bin/activate

Step 6: Install Ansible

pip install ansible

Step 7: Verify Ansible is installed.

ansible --version

Step 8: Open the default hosts file and define windows host specification.

vi /etc/ansible/hosts

Note: Make sure to have correct credentials placed in this file pointing to Windows host, it is to avoid connectivity issues.

Step 9: Install Pywinrm

pip install pywinrm

Note: WinRM (Windows Remote Management) is Microsoft’s implementation of WS-Management, a SOAP based protocol for management of devices and servers. It can be used to connect to remote Windows servers and run commands on them.

Following steps should be run on Windows:

Step 10: Download WinRM script on remote Windows host using the link below.

https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

Step 11: Run the WinRM script on Remote Windows host.

To Run WinRM script on Remote Windows host, open Windows PowerShell as the Administrator.

Now switch back to Centos.

Step 12: List of hosts

ansible all --list-hosts

Step 13: Check and test the connection between remote Windows host and Ansible control node.

ansible webserver -m win_ping

Step 14: Create a playbook and inside the playbook, create a task that will automate the services on the remote host. In this playbook we are using the Ansible win_service module to start and stop the services.

touch service.yml

Step 15: Execute the playbook.

ansible-playbook service.yml

The above output shows that the playbook has executed successfully. This implies that we can now automate the remote Windows host services using Ansible Playbooks. Follow below troubleshooting links if you encounter any issues.

Troubleshooting links:

Note: If you are using a local machine or VM from any one of the well known public cloud vendors, please make sure you have added below mentioned Inbound rules to avoid connectivity issues.

Conclusion:

Thus this article concludes on how to automate remote Windows host services using Ansible. This simple Ansible automation works wonders when we want to manage services of remote Windows hosts, when the scope of manual changes is limited. So if you are looking for a managed remote Windows host in cloud environments, Ansible is an ideal choice.

--

--