How To Setup An Ansible Test Lab For Windows Managed Nodes & Custom Windows Modules27 Jan 2015
On starting to look at learning using Ansible to manage Windows hosts, first step was to setup a test lab.
Setup an Ubuntu 14.04 or Cento7 and a Windows 2008 R2 virtual machines.
Install Ansible (Ubuntu)
Install Ansible (Centos7 Minimal)
Configure WinRM On Windows 2008 R2 Guest
The configuration of Windows managed guests WinRM is automated and available in this powershell script.
Create A Working Space Test Your Connectivity
Ansible is file based, so here I create a folder underneath my home directory to store my lab configuration
We then create a file for holding hosts in lab and begin editing it
Add the contents of this in the following format, adding extra lines with extra IPs as needed
We then create a file for holding WinRM connectivity hosts in lab and begin editing it
Add the contents of this in the following format, adding extra lines with username and password as needed
Once we have these two files setup, we can look to test connectivity
Debugging is not enabled by default, you might want to append -vvvvv to enable this if you have issue on first connect.
If you still have issues you can test connectivity using cURL
When you havethe win_ping module working, you can look at running the other modules shipped with the core product a full list can be found here. Maybe you might gather the Ansible facts using.
Create A Custom Powershell Module
On looking at the core modules you might think your a bit limited but its easy to wrapper your existing Powershell logic as an Ansible module.
I decided to keep all my custom modules in the lab in there own folder and change the module path.
An Ansible Powershell module is made up of two files, one is the ps1 with the script contents and one is py which has description and examples. The main difference appears to be to get the Powershell console output back to Ansible you need to form as object and convert that object to JSON. It also appears not required but good practice to set an object to be returned with a changed flag to true or false unsure but believe this logic might be used at runtime to decide whether to call the handler.
An easy way to create a new module, is to copy an existing one and rename it this way you get the supporting text and structure.
A simple example use case might be if you wanted to call the Get-Host cmdlet to gather Powershell version your ps1 might read.
Using the same example your py might read
Once we have these two module files, we can look to test the new module
If its working you should get returned the output from Get-Host command
With this technique you should be able to form some simple modules.