Created
May 25, 2014 12:37
-
-
Save kamazee/77371a41625bbaf3ec0c to your computer and use it in GitHub Desktop.
.bat-proxy to run ansible-playbook on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
set BASH_PATH=bash | |
set CYGPATH=cygpath | |
REM Well, here is the problem: | |
REM Vagrant ships a set of POSIX utils, including bash | |
REM And for this script environemnt contains | |
REM modified %Path% which contains Vagrant's bin prior to cygwin's one, | |
REM so call "bash" gives Vagrant's bash, not cygwin's | |
REM Bad! Let's set cygwin's bin prior to Vagrant's one back: | |
REM Getting bin path from cygpath which is *definitely* cygwin's | |
for /f "delims=" %%a in ('%CYGPATH% --windows /bin') do @set CYGWIN_BIN=%%a | |
set PATH_SPOILED_BY_VAGRANT=%Path% | |
set Path=%CYGWIN_BIN%;%Path% | |
REM Get current directory in cygwin's terms | |
for /f "delims=" %%a in ('%CYGPATH% --unix %CD%') do @set PWD=%%a | |
REM Current command | |
set CMD=%~n0 | |
REM Arguments as a string | |
set ARGS= | |
REM The point of having a loop here is avoiding %* | |
REM When using %* while formatting bash command to pass to Cygwin's bash | |
REM curly braces mysteriously disappear. This doesn't happen when the whole command | |
REM is represented as a string | |
REM As a bonus, quotes are escaped during processing to protect them from | |
REM being eaten by bash while interpreting arguments | |
:loop | |
if "%~1"=="" goto cont | |
set CURRENT_ARG=%~1 | |
REM Escape quotes | |
set CURRENT_ARG=%CURRENT_ARG:"=\\\"% | |
REM Merge with previous arguments | |
set ARGS=%ARGS% %CURRENT_ARG% | |
shift & goto loop | |
:cont | |
REM Run ansible-playbook in bash | |
"%BASH_PATH%" -l -c "cd %PWD%; ANSIBLE_SSH_ARGS='-o ControlMaster=no' %CMD% %ARGS%" | |
set Path=%PATH_SPOILED_BY_VAGRANT% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment