Getting Vagrant, Ansible and VirtualBoxes running under WSL

How I solved many hurldes spinning-up VirtualBox machines using Vagrant and Ansible under WSL behind a corporate proxy

Vagrant, Ansible and VirtualBox on WSL

I’ve finally succedded in installing and deploying three CentOS virtual boxes using Vagrant and Ansible from within WSL (Windows Subsystem for Linux) and behind a proxy!

Here I will highlight the main stumbling parts and the commands and/or environment variable to run and/or set in order to overcome one hurdle after the other in getting thigs running smoothly.

Warning: This is not an howto or a tutorial. If you tinkering with Vagrant and Ansible, I assume you know how to install and setup the whole environment…

Requirements:

  • For WSL to work, make sure the VT support is enabled in your BIOS
  • Vagrant: latest
  • Vagrant plugins:
    • vagrant-proxyconf needed for running behind proxy
    • virtualbox_WSL2 needed for running under WSL
    • vagrant-vbguest automatically installs the host’s VirtualBox Guest Additions
  • Ansible: latest
  • Virtualbox: latest

Ansible Environment:

Multi-Machine Vagrant Environments: This Vagrantfile will create 3 Centos VM’s to simulate Ansible control machine and 2 target hosts.

  • host1
  • host2
  • ansible-host

WSL Environment Setup

Bash environment:

1
2
export http_proxy=http://<your_proxy_server>:<port>
export https_proxy=https://<your_proxy_server>:<port>

VAGRANT Environment Setup

VirtualBox settings needed for Vagrant

1
2
3
4
5
6
7
export VBOX_USER_HOME="/mnt/c/Users/<username>/.VirtualBox/"
export machinefolder="/mnt/c/Users/<username>/.VirtualBox/"
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export VAGRANT_HOME="~/.vagrant.d/"
export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox" 
export VAGRANT_LOG=debug
set SSL_CERT_FILE=C:\My\App\Tools\HashiCorp\Vagrant\embedded\gems\gems\excon-0.99.0\data\cacert.pem

Plugin Installation

Install (some plugins might need to be installed as user, not root to work):

1
2
3
4
$ vagrant plugin install --plugin-clean-sources --plugin-source https://rubygems.org virtualbox_WSL2
$ vagrant plugin install --plugin-clean-sources --plugin-source https://rubygems.org vagrant-vbguest
  - or might be necessary: $ vagrant plugin install --provider virtualbox --insecure vagrant-vbguest
$ vagrant plugin install --plugin-clean-sources --plugin-source https://rubygems.org vagrant-proxyconf

git setup

1
git config --global http.sslverify false

Install requirements

1
ansible-galaxy install --ignore-certs  -r requirements.yml

Files included:

  • ansible.cfg
  • inventory
  • bootstrap-node.sh
  • ansible-install.yaml
  • playbook.yaml
  • inventory.yaml

Connecting the dots:

Before you run vagrant up, make sure that you updated the Vagrantfile to your desired configuration. Specifically use the box that are available from https://atlas.hashicorp.com/boxes/.

Once you are done:

  • vagrant up Wait a few minutes to finish the build. Once done. You can try to ssh to your ansible-host vm. You can verify this by using “vagrant status

  • vagrant ssh ansible-host once you are login to your ansible-host vm, you can now verify if the other vm are reachable.

Full code is available here: https://github.com/xserty/ansible-labs, but don’t forget to setup the environment!

Example code was taken from https://github.com/mikecali/ansible-labs-101 under GNU General Public License v3.0.