Ansible is a nice tool to manage your servers simply by using ssh. I'm working on scripts to deploy all my services via ansible but I run into a problem: Ansible isn't really well supported on Windows.
I had the choice between run it on mingw or run it in a virtual machine.
I decided to use a virtual machine but the next question was: Which Hypervisor should I use? Virtualbox? VMware Workstation? After getting some crazy errors on Virtualbox I just remembered that Windows just have a build in Hypervisor named HyperV. Yes, it's there even in the desktop version.
On Windows 8 and 10 you just open the "Software" configuration by using Windows-Key and R, insert
appwiz.cpl to the run window and press enter. Now you select
Turn Windows features on or off and check HyperV entry.
After pressing OK it'll tell you it has to restart (as always).
After a reboot HyperV is installed now and we can go further.
Setup your Network
Before you can create a network you need to access the HyperV-Manager. You can do this by typing
virtmgmt.msc into the run window and press enter.
The console should open and now you can select "Virtual Switch Manager" to configure the HyperV networking.
Networking in HyperV is a bit creepy but if you have no need to NAT your VM you can take the short Line
NAT with HyperV
To use a network in HyperV you need to define a network switch for your VMs. If you want to NAT your VM you first have to setup a internal network interface. Name it as you wish it's not important.
After having the internal switch up, go to Network Connections.
You can do this by typing
control netconnections to your Run-window, which can be opened by using Windows-key and R.
Select the connection you want to NAT to and just go to Properties. Select "sharing" and check the first box. Now select the internal network device you've created in HyperV from the drop-down menu and click on apply.
You can also just create an external network switch which allows the virtual to access your physical network directly. Give it a name and it's done.
Setup a VM
In my case I wanted to use debian in a core install, you can use any other OS which works on HyperV and with ansible.
Open the HyperV-managment-console by using
virtmgmt.msc in the run window.
Now select "New" > "Virtual Maschine" to start. Just follow the setup and select your created network switch device.
Download a linux image like debian netinstall and select it inside the wizard when you're asked for a Operating System.
After clicking on "Finish" you can start the VM.
Install debian and ansible
Well, it's a default install, follow the Setup Wizard and install it as you wish. I preferred to install a OpenSSH server but without Desktop Environment.
After having the OS installed on your VM log in as root.
Change your machine's IP to a static one. If you use the NAT solution you have to use an IP address in 192.168.137.0/24. I used 192.168.137.2.
# /etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface allow-hotplug eth0 iface eth0 inet static address 192.168.137.2 netmask 24 gateway 192.168.137.1 dns-nameservers 188.8.131.52 184.108.40.206
Now you should add your SSH key and permit password based login by SSH. After running
apt-get update && apt-get install ansible your ansible machine is ready.
To get a more recently version of ansible you should use debian backports.
Now you can use Putty and pagent to login to the VM. And using pagent allows you to pass SSH keys thought the VM to the systems you want to manage.
If you want to do that you need to enable it in Putty in
Connection > SSH > Auth by using the
allow agent forwarding option.
After connecting using Putty you can run
ansible localhost -m ping.
Using Ansible on Windows in a clean way isn't easy but using HyperV, Debian, Ansible and SSH is a nice working solution without the need to install much software on Windows.
You should may notice the Ansible version of Debian is quiet old. I'm currently working on switching to Ubuntu where it's up to date.
But the most important point is: It just works!
As always you can comment to the entry, share it on social media using those lovely buttons or just say hi on Twitter to me. Hope you enjoyed it and more Tutorials about Ansible or Windows are coming soon!
Some more informations about HyperV basics
- Install/Enable HyperV on Windows 8/10
- Official virtual switch documentation
- More about virtual switches
- Official Virtual Maschine documentation
A really useful webpage for some Windows shortcuts.
Some more Information about setup Ansible.