├── ansible-playbook.bat ├── README.md └── ansible-playbook-shim.sh /ansible-playbook.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | REM Vagrant ships a set of POSIX utils, including bash 4 | REM And for this script environemnt contains 5 | REM modified %Path% which contains Vagrant's bin prior to cygwin's one, 6 | REM so call "bash" gives Vagrant's bash, not cygwin's 7 | 8 | REM Getting bin path from cygpath which is *definitely* cygwin's or babun's bin 9 | for /f "delims=" %%a in ('cygpath --windows /bin') do @set CYGWIN_BIN=%%a 10 | 11 | REM Set SH to Cygwin/Babun Shell, so we get over Vagrant 12 | set SH=%CYGWIN_BIN%\zsh.exe 13 | 14 | echo "Windows Ansible Shim in Action..." 15 | 16 | REM Ugly quotations support patch 17 | set v_params=%* 18 | set v_params=%v_params:"--=--% 19 | set v_params=%v_params:"}"="}% 20 | set v_params=%v_params:"=\\\"% 21 | 22 | "%SH%" -c "ansible-playbook-shim.sh %v_params%" 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ansible-playbook-shim 2 | 3 | This is a working shim which allows using ansible provisioner with Vagrant 1.7.3+ on a Windows OS. 4 | Installing Ansible on Windows is not covered by this help. Please refer to any of the multiple tutorials 5 | in internet, for example [this one](https://chrisgilbert1.wordpress.com/2015/06/17/install-a-babun-cygwin-shell-and-ansible-for-windows/) 6 | 7 | 8 | # Usage 9 | 10 | We assume you have Cygwin or Babun installed. 11 | 12 | Clone the latest source and create a symlink in your cygwin path space 13 | ``` 14 | git clone https://github.com/rivaros/ansible-playbook-shim.git 15 | ln -s $PWD/ansible-playbook-shim.sh /usr/local/bin/ansible-playbook-shim.sh 16 | ``` 17 | 18 | Copy ansible-playbook.bat somewhere within your Windows path. 19 | For example C:\Users\%username%\\.babun\ 20 | 21 | 22 | # How it works 23 | 24 | The ansible-playbook.bat file is called by Vagrant provisioner instead of the original ansible-playbook. 25 | This allows us to replace the default Vagrant shell with Babun/Cygwin. 26 | 27 | Later ansible-playbook-shim.sh comes into action. Script analizes the parameters for inventory file 28 | location, extracts the private keys to cygwin home folder, which makes it possible to use them 29 | with Babun ansible installation. 30 | 31 | Any comments/PRs are welcome. 32 | 33 | ... 34 | 35 | 36 | -------------------------------------------------------------------------------- /ansible-playbook-shim.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This shim works only with modern versions of Vagrant 1.7.3 + 4 | # 5 | 6 | echo "Parameters received: $@" 7 | 8 | # Get inventory file from ansible-playbook parameters 9 | INVENTORY_FILE=`echo $@ | sed -E 's/.*--inventory-file=([^ ]*).*/\1\/vagrant_ansible_inventory/'` 10 | 11 | # Process inventory file 12 | # Copy private keys to Cygwin/Babun folders, change permissions 13 | # Modify original inventory file with new key pathes 14 | while IFS='' read -r line; do 15 | if [[ $line == *"ansible_ssh_private_key_file"* ]] 16 | then 17 | HOSTNAME=$(echo $line | sed -E 's/([^ ]*)([ ]+).*/\1/') 18 | ORIG_PRIV_KEY=$(echo $line | sed -E 's/.*ansible_ssh_private_key_file=([^ ]*).*/\1/') 19 | #copy key files to host folders, chmod files 20 | mkdir -p $HOME/.ssh/vagrant/$HOSTNAME 21 | NEW_PRIV_KEY="$HOME/.ssh/vagrant/$HOSTNAME/private_key" 22 | cp $(cygpath -u $ORIG_PRIV_KEY) $NEW_PRIV_KEY 23 | setfacl -s user::r--,group::---,other::--- $NEW_PRIV_KEY 24 | echo `echo $line | sed -E "s|ansible_ssh_private_key_file=([^ ]*)|ansible_ssh_private_key_file=$NEW_PRIV_KEY|"` 25 | else 26 | echo $line 27 | fi 28 | done < $INVENTORY_FILE > inventory.tmp 29 | cp inventory.tmp $INVENTORY_FILE && rm inventory.tmp 30 | 31 | 32 | ANSIBLE_SSH_ARGS=`echo $ANSIBLE_SSH_ARGS | sed -E "s|ControlMaster=([^ ]*)|ControlMaster=no|"` 33 | ANSIBLE_SSH_ARGS=`echo $ANSIBLE_SSH_ARGS | sed -E "s|-o ControlPersist=([^ ]*)||"` 34 | 35 | # Disable ControlMaster, as it's not working on Windows 36 | export ANSIBLE_SSH_ARGS=$ANSIBLE_SSH_ARGS 37 | 38 | # Finally run ansible-playbook with original parameters 39 | ansible-playbook $@ 40 | 41 | 42 | --------------------------------------------------------------------------------