Ansible + Windows

Manage Windows with Ansible – Part 1: Setup Ansible Server on Ubuntu

In this article I will show you how to setup Ansible on Ubuntu. The goal is to manage Windows with ansible in my environment later. I use it to restore my Citrix Lab in case something goes wrong.

Since this is my first blog entry in English, I ask you to watch out for spelling mistakes. Writing is something else than talking 🙂

Automate everything! A saying I love. I’ve been a friend of it since I’ve been working in large customer environments. It starts with the fact that I prefer to use scripts instead of clicking my way through constantly changing GUIs. I am totally excited about DevOps and Datacenter Automation. So I decided to write a blog series about this topic and share my playbooks with you.

First steps in Automation

Anyone who deals with automation and configuration management will sooner or later also meet Ansible. Ansible is an open source automation tool. A big advantage in my opinion is that no agent must be used here. I’ve picked out some comparisons to other tools for you. For the moment I think this comparison is enough, maybe I will write an article later in which I compare the solutions from my point of view.

https://www.devopsgroup.com/blog/puppet-vs-ansible/

https://www.edureka.co/blog/chef-vs-puppet-vs-ansible-vs-saltstack/

Can that be love?

After I did some things with Ansible in the last view weeks I can tell you that this thing is really awesome!

What is supported?

Actually every common system is supported. All kinds of Linux distributions, Windows, Azure, AWS (there was a lot going on in the last project), but also a whole range of appliances and other cloud services.

Let the show begin!

First you download Ubuntu Server. I used Ubuntu Server 18.04.2 LTS. The download link can be found here:
https://ubuntu.com/download/server

You can go through the installation as shown in the pictures. I also installed the OpenSSH server, because I need it later anyway.

Important note: If you use vmware esxi 6.5 in conjunction with Ubuntu 18.04.x, the VM may crash again and again. I had this error in my lab. According to vmware KB the problem is fixed with esxi 6.5 U1. If you can’t update, you can find the KB article here: https://kb.vmware.com/s/article/2151480

When the installation of Ubuntu is complete, restart the server and continue with the installation of Ansible.

Setup Ansible Server

After we have gone through the basic installation of Ubuntu Server, we perform the setup of Ansible on the server. First we specify the repository.

Copy to Clipboard

Then we update the package index.

Copy to Clipboard

Once the package index has been updated, we can install Ansible. All you have to do is confirm with “yes / y” that the space on the disk can be used and the changes are made to the system.

Copy to Clipboard

During the installation of Ansible, you will be prompted to restart installed services on the system. You have to confirm this with “yes”. After that the setup process will continue.

Ansible Package Configuration

Once we setup Ansible, we will install python. Here you have to confirm the query for the disk space with “yes” or “y” again.

Copy to Clipboard

Additionally we install the WinRM module for python. This allows us to manage our Windows machines via WinRM and to run the Playbooks on the remote systems.

Copy to Clipboard

The required services are now installed on our Ubuntu server.

Configure Ansible Environment

So that Ansible knows where to find the configuration files, we have to adjust the “ansible.cfg”.

In the config file you have to change the inventory path to the desired path. Since I want to upload the Playbooks etc. via Visual Studio Code (article to follow) later, I’ll set the userhome of my user here.

In my case this is: /home/tom/lab/inventory

The path is made up as follows:

/home/Username/Environment (could be more than one)/inventory

You can open and edit them with nano as follows:

Copy to Clipboard
Install Ansible - Ansible Config

Once the Config for Ansible is adjusted, we restart the server once.

Normally I would create the Ansible folder structure on the Ansible server. This looks normal:

Copy to Clipboard

How the individual files look like I explain below still exactly.

Set up Windows Server for management

Actually you can now directly manage a Windows server with it. All you have to do is run this Powershell script on the server you want to manage:

https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

The script configures WinRM for you. You can customize it for your needs.

I have extended mine a little bit. I also configure the Windows Firewall.

File structure of the Ansible Environment

In order to manage our environment and run the playbooks on the correct maschine, Ansible needs some files to get the necessary information.

Copy to Clipboard

You can group your servers here. It is important that the group name is used later case sensitive in the playbooks.
A system can also be stored in several groups. You can use the FQDN (your Ansible Server must be able to resolve the DNS name for this) or the IP address.

The files in the folder group_vars
The group-specific parameters and information are stored in the group_vars. For example, the user and password that Ansible is supposed to use. I will show you as an example the file “windows” out of the folder group_vars. In my case such a file looks like this:

Copy to Clipboard

What we still need is a Playbook. I have chosen a simple one for the beginning. The playbook simply creates a folder structure on the target systems. Here you can also see that the group “windows” was always written in small letters like in the inventory and also in the group_vars file.The Ansible module win_file (https://docs.ansible.com/ansible/2.4/win_file_module.html) is used here.

Copy to Clipboard

Let it run!

Once you have setup Ansible and created the files, you can try out the Playbook directly. You can simply start this Playbook with the following command:

Copy to Clipboard

I very much hope you enjoyed the article. In the next article I’ll explain how you can create the playbooks in Microsoft Visual Studio code and run them from there on the Ansible server.

So have a nice day and leave a comment or like if you enjoyed. 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *