Finally Running Ansible from Windows…kinda.
Disclaimer: I consider everything below to be a hack. It works but it is a hack for sure. Enjoy
Not according to Ansible…
For many years I have been frustrated with the inability to run Ansible playbooks natively in Windows. Yes, Ansible works well in Windows Subsystem for Linux (WSL), but that comes with some limitations. Well one major limitation for myself. I could not use ansible as a provisioner in Packer from Windows. This is especially problematic if you want to build Hyper-V based images from WSL. To the best of my knowledge, you cannot do this because the user Packer is running as, needs to be in the 'Hyper-V Administrators' or 'Administrators'
groups.
So what do you do???
Honestly, I have given up on running Ansible on Windows many times over, but then something would pop up and I would try to make it work and fail and give up again. I never liked the idea of running packer, then spinning the VM back up to run ansible - there had to be a better way. Well, a couple of weeks ago I needed to try to make Packer, Ansible, and Hyper-V play nice on a Windows host, so I dove back in.
In my research to see if someone else had managed to figure out a decent solution, I stumbled upon a GitHub gist that looked very promising. It did not take me long to realize what they were doing and how it all worked. That gist can be found here.
What user zaenk had done was create a collection of batch scripts that when put in the PATH
on a Windows machine, would be called from Packer during the provisioning process, translate the slashes appropriately from Windows to Linux and passing everything as a command argument to Bash.exe. This was the missing link for me. A way to call ansible from WSL while in the Windows context.
I copied the .cmd
files to my local machine and added them to my PATH and fired off a packer build. Sadly, it was not long before things started to error, and I was not getting the results I had hoped for.
After a bit of troubleshooting, mainly have the batch script ansible-playbook.cmd
output what it was passing to C:\Windows\System32\bash.exe -c
as a command argument to a text file. Upon doing this it became clear that there was a lot of issues with escaping characters and others not being properly escaped. Eventually I came up with a version of ansible-playbook.cmd
that was passing the now escaped arguments from Packer to ansible-playbook
within WSL.
This is what I came up with for and ‘alias’ for ansible-playbook
@echo off
REM "alias" for packer ansible provisioner running in cmd/powershell
set args=%*
REM replace backslashes with slashes
call set args=%%args:\=/%%
REM fix drive letters
call set args=%%args:C:=/mnt/c%%
REM call set args=%%args:D:=/mnt/d%%
REM call set args=%%args:E:=/mnt/e%%
call set args=%%args:/"=%%
call set args=%%args:"'='%%
call set args=%%args:'"='%%
call set args=%%args:"=^'%%
C:\Windows\System32\bash.exe -c "ansible-playbook %args%"
Flow:
ansible-playbook.cmd
is ran when Packer calls ansible-playbook
ansible-playbook.cmd
rewrites all the slashes and escapes all the quotes necessary for WSLansible-playbook.cmd
Runs bash.exe and passes escaped arguments from Packer to ansible-playbook
in WSL where normal execution starts.There are some issues with how the output is displayed from ansible-playbook and I will look into resolving those but for the time being, this solution works and meets my needs. Just wish I had it 3-4 years ago.
Can be found on Github including a script for running ansible stand-alone.
Disclaimer: I consider everything below to be a hack. It works but it is a hack for sure. Enjoy
Today, October 5th, 2021, Windows 11 was officially released. While there has been much debate and angst about the system requirements to run the newest OS o...
First off, it has been way too long since I have done anything on this blog. I have been very busy with lots of things but this is not the time for that. Gue...
“One cannot expect to attain the highest point of exellence without hard work and perseverance.[…] Try to improve to some extent each day and to experienc...
I am literally a fraction of who I once was and that is amazing…
“One cannot expect to attain the highest point of excellence without hard work and perseverance.[…] Try to improve to some extent each day and to experien...
“One cannot expect to attain the highest point of exellence without hard work and perseverance.[…] Try to improve to some extent each day and to experienc...
Ok so it had been over like 15 months since I posted anything…which is not surprising as my laziness far outwieghs my desire to share things with the masses…...
It was such an honor to be able to attend the inaugural All Things Open conference in Raleigh, North Carolina last week. The event hosted of one of the be...
Day Two at All Things Open was insane.
Let me start by saying job well done to all people involved in making this event happen! WOW it is so good.
rm -rf /Work/Day/Monday mkdir /Work/Day/Monday /Work/Day/Monday/Modules cp /Weekend/Modules/Energy /Work/Day/Monday/Modules service Monday restart</pre&g...
All Things Open 2013