Example ansible playbook - showing how to provision, deploy and run a Spring Boot app as a Windows Service using Ansible, chocolatey & nssm
There´s a blog post with more background information here: Running Spring Boot Apps on Windows with Ansible (codecentric.de)
Isn´t Ansible SSH-only?
From Version 1.7 on, Ansible also supports managing Windows machines. This is done with native PowerShell remoting (and Windows Remote Management WinRM) instead of SSH, as you can read in the docs.
Go to https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/#downloads and download an Vagrant image with Windows 10 (e.g. for VirtualBox - be sure to have the VM-Provider installed). You should get something like a MSEdge.Win10_RS1.Vagrant.zip - extract it (Mac: with the Unarchiver) and there you are: The Windows Vagrant box dev-msedge.box is ready :)
Because Microsoft doesn´t seem to ship metadata for the box, add it to Vagrant via:
vagrant box add dev-msedge.box --name "windows10"
I added a Vagrantfile to this repository, so you can start right away by (if you have Vagrant installed ;) )
vagrant up
Because we use Windows, SSH for example will not work and we need to tweak some Vagrant Configuration Options described here: https://www.vagrantup.com/docs/vagrantfile/winrm_settings.html
Doku: http://docs.ansible.com/ansible/intro_windows.html#windows-system-prep
If you have Windows >= v10, just skip the follwing steps & proceed with Configure remote access for ansible
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
If get-host shows something < 3.0, you should upgrade with https://github.com/cchurch/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1 (this will reboot your Windows box! )
On a Powershell with Admin rights, run:
iwr https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -UseBasicParsing | iex
(or if that doesn´t work (e.g., if your Powershell Version does not know iwr), download the Script https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 and run it manually from Powershell).
ansible restexample-windows-dev -i hostsfile -m win_ping
If this brings you something like the following output, your Windows Box is ready for Ansible!:
127.0.0.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
If you have something like
127.0.0.1 | UNREACHABLE! => {
"changed": false,
"msg": "ssl: the specified credentials were rejected by the server",
"unreachable": true
}
then your UserName or Password is not correct - and trust me, double or tripple check this before moving to the next point :)
If you get a read time out, your Windows Box is most likely not configured correctly:
127.0.0.1 | UNREACHABLE! => {
"changed": false,
"msg": "ssl: HTTPSConnectionPool(host='127.0.0.1', port=55986): Read timed out. (read timeout=30)",
"unreachable": true
}
check, if you already successfully ran Configure remote access for ansible
Just take the simple project here: https://github.com/jonashackt/restexamples
Either way you choose: Be sure to have a working Build in Place, so that you have a runnable Spring Boot jar-File in place (e.g. restexamples-0.0.1-SNAPSHOT.jar). For the example project restexamples you get this by running:
mvn clean package
I did that step already for you :) So let´s run our the playbook restexample-windows.yml:
ansible-playbook -i hostsfile restexample-windows.yml --extra-vars "spring_boot_app_jar=../restexamples/target/restexamples-0.0.1-SNAPSHOT.jar spring_boot_app_name=restexample-springboot host=restexample-windows-dev"
- ALWAYS: escape \ in paths with "" --> e.g. C:\\temp
- don´t assume that a path with C:\ProgamFiles (x86)\XYZ will work (e.g. for Java better use "C:\\ProgramData\\Oracle\\Java\\javapath\\java.exe")
- if chocolatey doesn´t want to work, you have to install it once manually on your Windows box
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex