Skip to content

Instantly share code, notes, and snippets.

@travelhawk
Last active October 18, 2023 14:55
Show Gist options
  • Save travelhawk/b9c1b6b7a9cd35cf3898fcb33cdae72d to your computer and use it in GitHub Desktop.
Save travelhawk/b9c1b6b7a9cd35cf3898fcb33cdae72d to your computer and use it in GitHub Desktop.
Use vagrant and ansible on Windows (WSL2)

Use vagrant and ansible on Windows (WSL2)

Making vagrant and ansible work together with virtualbox as a provider is not well documented across the documentations. Ansible's control node doesn't work on Windows. However, it works on WSL which make the installation of ansible and vagrant on WSL necessary and the access from WSL to virtualbox which is still running under the Windows host.

Requirements

  • Windows 10
  • Virtualbox
  • WSL2

Install Vagrant on WSL

Install Virtualbox

Install Virtualbox and the Extension Pack from here: (https://www.virtualbox.org/wiki/Downloads)

Install Vagrant

Enter WSL and install the latest version of Vagrant using the following code

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant

In order to ssh into a machine from WSL, install the Virtualbox_WSL2 plugin: vagrant plugin install virtualbox_WSL2

You must also enable WSL2 support by adding the following lines to the .bashrc:

### .bashrc
# Setup Vagrant variables
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/Users/YOUR_USERNAME/"
export PATH="$PATH:/mnt/c/Program Files/VirtualBox""

Replace YOUR_USERNAME with the Windows username.

Install Ansible

Enter WSL and install the latest version of Ansible using pip:

sudo apt-get install python3-pip
python3 -m pip install --user ansible

Prevent permission issues

In order to prevent permission issues, you need to create /etc/wsl.conf inside the WSL distribution with the following content:

# Enable extra metadata options by default
[automount]
enabled = true
root = /mnt/
options = "metadata,umask=77,fmask=11"
mountFsTab = false

Additional Issues

  • You might need to allow your firewall for Virtualbox for private and public networks
  • You might need to update your Powershell version on the host
  • If there are error message that you cannot access cmd.exe from WSL2, add the following to your .profile:
    PATH="/mnt/c/Windows/System32:$PATH"
    
  • If you experience an issue with an outdated powershell version, there are some things to try
    • Install the newest powershell version
    • Check the /opt/vagrant/embedded/gems/gems/vagrant-2.3.7/lib/vagrant/util/powershell.rb
    • Add the powershell version you are using to the PATH variable
    PATH="$PATH:/mnt/c/Program Files/PowerShell/7-preview"
    

VERR_PATH_NOT_FOUND when using vagrant up

I received the following error a few times:

There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "95ea3498-832e-49b7-a351-ecc0f161b4b8", "--type", "headless"]

Stderr: VBoxManage.exe: error: RawFile#0 failed to create the raw output file /mnt/d/DevProjects/work/btlsw/services/reliable-filetransfer/ubuntu-bionic-18.04-cloudimg-console.log (VERR_PATH_NOT_FOUND)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

It seems that this is due to the activated Serial Port. After deactivating it via an option for the Virtualbox provider it worked. Add vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ] to your Vagrantfile like below.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.provider "virtualbox" do |vb|
    vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
  end

Docker Desktop and Virtualbox

If you use Docker Desktop and Virtualbox together, Virtualbox will be very slow (turtle mode). The turtle mode can be prevented when disabling the hypervisor. For Docker it needs to be enabled though otherwise Docker doesn't work.

Disable Hypervisor

bcdedit /set hypervisorlaunchtype off

Enable Hypervisor

bcdedit /set hypervisorlaunchtype auto

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment