Vagrant box not getting set up running under WSL2

1.8k views Asked by At

Vagrant version

Vagrant 2.2.10

Host OS

Windows 10 (20045, May 2020)

Guest OS

Ubuntu 18.04 (tried with 16.04, 20.04)

Vagrantfile

Vagrant.configure("2") do |config|
 config.vm.box = "ubuntu/trusty64"

 config.vm.synced_folder ".", "/vagrant", disabled: true

 config.vm.provider "virtualbox" do |vb|
        vb.memory = "4024"
        vb.cpus = "1"
end

config.vm.provision "ansible" do |ansible|
       ansible.become = true
       ansible.verbose = "v"
       ansible.playbook = "roles/firewall.yml"
       ansible.galaxy_roles_path = "./roles"
end end

Error:

U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' version '20190514.0.0' is up to date...
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...
PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/mnt/c/Users/KeithJames/firewall-role-ak/roles' ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit="default" --inventory-file=/mnt/c/Users/KeithJames/firewall-role-ak/.vagrant/provisioners/ansible/inventory --become -v roles/firewall.yml
Using /etc/ansible/ansible.cfg as config file

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 2222: Connection refused", "unreachable": true}

PLAY RECAP *********************************************************************
default                    : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

Expected behavior

The VirtualBox VM should be started, Vagrant should be able to connect to it (instead I get

default: Warning: Connection refused. Retrying...

over and over, and I can't connect via SSH).

Actual behavior

Vagrant inside WSL2 can't connect to the VM it just started.

U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$ service ssh status * sshd is running

Steps to reproduce

  1. Install WSL2 on Windows 10 (2004), with Ubuntu bash

  2. Create a directory with the above Vagrantfile in it

  3. Download and install Vagrant for Linux in the WSL2 environment (regardless of it being installed in Windows)

  4. Run vagrant up

I also tried:

Run vagrant ssh in WSL2.

U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$ vagrant ssh
U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$

Then I tried using raw SSH:

U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$ ssh -p 2222 [email protected]
ssh: connect to host 127.0.0.1 port 2222: Connection refused


U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' version '20190514.0.0' is up to date...
==> default: Setting the name of the VM: firewall-role-ak_default_1602247434343_82880
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...
    default: Warning: Connection refused. Retrying...

Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly, as well.

If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.

U9HRRBMV:/mnt/c/Users/KeithJames/firewall-role-ak$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' version '20190514.0.0' is up to date...
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...
PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/mnt/c/Users/KeithJames/firewall-role-ak/roles' ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit="default" --inventory-file=/mnt/c/Users/KeithJames/firewall-role-ak/.vagrant/provisioners/ansible/inventory --become -v roles/firewall.yml
Using /etc/ansible/ansible.cfg as config file

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [default]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 2222: Connection refused", "unreachable": true}

PLAY RECAP *********************************************************************
default                    : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.

VirtualBox

VirtualBox Vagrant Machine

2

There are 2 answers

1
Pedro de Paula On

As HashiCorp say, you shouldn't install vagrant on WSL. You have to use vagrant.exe from Windows. https://www.vagrantup.com/docs/other/wsl

0
Rocha6464 On

I was able to get this running on my environment (Windows 10 Pro version 22H2, VirtualBox version 7.0.6 (installed on Windows), Ubuntu 20.04.5 LTS (running in WSL2)) by doing the following:

  1. Execute the instructions provided at https://blog.thenets.org/how-to-run-vagrant-on-wsl-2/ .
  2. Modify Windows Defender firewall settings as described at https://github.com/microsoft/WSL/issues/4139#issuecomment-732665787 .
  3. Modify Windows Defender firewall settings as described in the section "Fixing Issues" in https://thedatabaseme.de/2022/02/20/vagrant-up-running-vagrant-under-wsl2/ .

Residual issue:

Windows issues a warning "Microsoft Defender Firewall is using settings that may make your device unsafe."