Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appendix A - Vagrant with VirtualBox within WSL2 to run ansible-for-devops VMs #291

Open
geerlingguy opened this issue Jun 24, 2020 · 35 comments

Comments

@geerlingguy
Copy link
Owner

geerlingguy commented Jun 24, 2020

I think it may finally work, but there are some caveats.

Following my work on Episode 14 of Ansible 101 (see #289), I found you have to do the following:

  1. Install WSL2, and install Ubuntu in WSL2.
  2. Install VirtualBox (I'm running 6.1.x).
  3. Inside WSL2/Ubuntu/Bash environment, install Vagrant:
    a. wget https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_linux_amd64.zip
    b. unzip vagrant_2.2.9_linux_amd64.zip
    c. sudo mv ./vagrant /usr/local/bin/vagrant
    d. export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
    e. vagrant --version1
  4. cd into a /mnt/c directory (like /mnt/c/Users/jgeerling/Downloads) and clone or otherwise put the ansible-for-devops repository contents in there.
  5. cd into one of the ansible-for-devops example directories (e.g. drupal).
  6. Edit the Vagrantfile and make sure to disable the default shared folder (add line config.vm.synced_folder '.', '/vagrant', disabled: true).2
  7. Run vagrant up from that directory.

1 It seems that every time I run a vagrant command, the Bash terminal window is reverted to 'raster' fonts at a very tiny size, and I have to go into the window preferences to revert back to Consolas / 24 pt... Very weird.

2 There's an issue for supporting shared folders within the WSL2 environment here. This might not be needed in the future.

@geerlingguy
Copy link
Owner Author

Well that's odd. That weird bug is due to some strange issue (microsoft/terminal#280 (comment)) in the console's font checking that drops it out from using Consolas. The solution is to use Lucida instead. Weird flex, but ok?

@geerlingguy
Copy link
Owner Author

I was getting 'Connection refused' for SSH, so I tried setting a different port for SSH in the Vagrantfile:

config.vm.network :forwarded_port, guest: 22, host: 4222, id: 'ssh'

Then on next start, the Windows firewall seemed to notice the difference, so I allowed the connection:

Screen Shot 2020-06-23 at 10 23 43 PM

But I still couldn't get an SSH connection on that port. I wonder if it's a windows/WSL networking issue :/

@geerlingguy
Copy link
Owner Author

I think... unfortunately, I'm going to have to give up on this for now at least. I was so close. Vagrant builds the VM, I can see it's running and booted, but it doesn't seem like a private IP / host-only network is configured correctly (e.g. the drupal example sets up 192.168.88.8, but that IP didn't show as reachable when I booted up that VM). And whether I used port 2222 or 4222 SSH could never connect (either via vagrant ssh or via ssh -p).

@geerlingguy
Copy link
Owner Author

Adding v.gui = true in the virtualbox config in the Vagrantfile allows me to log into the machine manually, though if I do that and check things out, sshd is running, but there's definitely not a private host network started up on 192.168.88.8... so I'm not sure what's going wrong with Vagrant/VirtualBox networking here. (Also, screen is teensy tiny.)

Screen Shot 2020-06-23 at 10 49 43 PM

@geerlingguy
Copy link
Owner Author

Yeah... so I give up for now. I was so close, too!

@geerlingguy
Copy link
Owner Author

I installed the same version of Vagrant in Windows using the MSI installer, and it looks like the private network is set up / attached correctly:

Screen Shot 2020-06-24 at 8 47 02 AM

@geerlingguy
Copy link
Owner Author

Opened new upstream issue: hashicorp/vagrant#11716

@geerlingguy
Copy link
Owner Author

More attempts:

Created new inventory file with contents:

[default]
172.16.88.8 ansible_user=vagrant ansible_ssh_private_key_file=~/.ssh/insecure_private_key

Modified Vagrantfile and added to the ansible config.vm.provision configuration:

ansible.inventory_path = "inventory"

Copied insecure_private_key from Windows side to Linux so I could control permissions (otherwise it's always read as 0777):

$ cp /mnt/c/Users/jgeerling/.vagrant.d/insecure_private_key ~/.ssh/insecure_private_key
$ chmod 600 ~/.ssh/insecure_private_key

Ran vagrant provision and now:

$ vagrant provision
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

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

TASK [Gathering Facts] *********************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host 172.16.88.8 should use
 /usr/bin/python3, but is using /usr/bin/python for backward compatibility with
 prior Ansible releases. A future Ansible release will default to using the
discovered platform python for this host. See https://docs.ansible.com/ansible/
2.9/reference_appendices/interpreter_discovery.html for more information. This
feature will be removed in version 2.12. Deprecation warnings can be disabled
by setting deprecation_warnings=False in ansible.cfg.
ok: [172.16.88.8]

TASK [Update apt cache if needed.] *********************************************
changed: [172.16.88.8]

TASK [Get software for apt repository management.] *****************************
changed: [172.16.88.8]

TASK [Add ondrej repository for later versions of PHP.] ************************
changed: [172.16.88.8]
...

So... yay! But this is quite a clunky way to do things. But at least I could get it to work.

@geerlingguy
Copy link
Owner Author

geerlingguy commented Jun 24, 2020

I randomly get the following error about every third playbook run from within WSL2:

TASK [Install Node.js and npm.] ************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: No module named ansible.module_utils
fatal: [192.168.55.55]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.55.55 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1593008691.439251-2336-205927529649911/AnsiballZ_yum.py\", line 104, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1593008691.439251-2336-205927529649911/AnsiballZ_yum.py\", line 96, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1593008691.439251-2336-205927529649911/AnsiballZ_yum.py\", line 39, in invoke_module\r\n    from ansible.module_utils import basic\r\nImportError: No module named ansible.module_utils\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

It seems to maybe be yet another weird filesystem related issue? Or maybe networking between WSL2 and the Windows host? It never happens on the same task twice.

I'm also getting checksum errors, too, for example in NPM:

npm ERR! shasum check failed for /tmp/npm-4183-1105ef61/registry.npmjs.org/winston/-/winston-0.8.3.tgz
npm ERR! Expected: 64b6abf4cd01adcaefd5009393b1d8e8bec19db0
npm ERR! Actual:   7654c0b56ea856b2ee6010ef593d9d189b239a87
npm ERR! From:     https://registry.npmjs.org/winston/-/winston-0.8.3.tgz

And then on the next run I got the warning:

TASK [Install Node.js and npm.] ************************************************
[WARNING]: sftp transfer mechanism failed on [192.168.55.55]. Use
ANSIBLE_DEBUG=1 to see detailed information
ok: [192.168.55.55]

Also getting the following for most Git operations (though sometimes they succeed?!):

TASK [Check out drush 8.x branch.] *********************************************
fatal: [172.16.88.8]: FAILED! =>

cmd:
/usr/bin/git clone --origin origin https://github.com/drush-ops/drush.git /opt/drush

msg:
Cloning into '/opt/drush'...
error: inflate: data stream error (incorrect data check)
fatal: pack has bad object at offset 322670: inflate returned -3
fatal: index-pack failed", "rc": 128, "stderr": "Cloning into '/opt/drush'...
error: inflate: data stream error (incorrect data check)
fatal: pack has bad object at offset 322670: inflate returned -3
fatal: index-pack failed

If anyone says Windows now has 'as seamless of Linux integration as macOS' I think I might slap them with a trout 🐟 .

@gchazot
Copy link

gchazot commented Jun 25, 2020

I've gone through the same trouble trying to get Vagrant to work on WSL2.

I think the core problem at the moment though is that VirtualBox doesn't really work with Hyper-V even if they pretend to. To highlight that. just try to create manually a virtual machine from the VirtualBox UI and install Ubuntu on it. You'll see that the box keeps failing randomly during install (I tried a few times, with different ISOs and the install most often reports corrupted files on the media) or later (I managed to go through the install process once and then it was hanging or APT would report corrupted downloads with a different hash evey time). I think this discussion on the VirtualBox forum sums it up.

I'm keen to give Microsoft and VirtualBox a couple of months to sort that stuff out since WSL2 has just come out for the general public. But if I get the time I'll follow these few leeds to try to progress:

  • Try older versions of VirtualBox (I've only tried 6.1.10) that claim to support cohabitation with Hyper-V
  • Try to use hyper-v virtual machines with Vagrant installed in WSL2.
  • Try to use hyper-v virtual machines with Vagrant installed on windows directly.

@gchazot
Copy link

gchazot commented Jun 25, 2020

The current state of this is quite strange as my development stack is mostly relying on Docker + Vagrant/VirtualBox.

  • With WSL1, the setup didn' t require Hyper-V. The solution was to run docker-machine (basically a VirtualBox VM running Docker) and Vagrant worked well from WSL with VirtualBox on Windows, after following the configuration steps from Vagrant docs. Docker in a VM was a bit of a pain though: slow and complex because of the nesting in the VM (can' t use localhost to address it for example).
  • With WSL2, you need Hyper-V to run it anyway, so you get the benefit of the new WSL2 backend of Docker for Windows. But now it's Vagrant/Virtualbox that' s being a pain.

So my current situation is I'm running Linux (as the host OS) for development...

@geerlingguy
Copy link
Owner Author

Also found this today:

The one big fly in the ointment is that there's currently no bridge mode available for your WSL2 VM's network interface. The way things work now, your Windows 10 PC creates a virtual subnet, with NAT between it and the LAN on the Windows 10 PC's real network interface. Your WSL2 VMs then get addresses within that virtual subnet, assigned by DHCP.

From What’s new in Windows 10 Build 2004

@geerlingguy geerlingguy changed the title Using Vagrant with VirtualBox within WSL2 to run ansible-for-devops VMs Appendix A - Vagrant with VirtualBox within WSL2 to run ansible-for-devops VMs Jul 21, 2020
@geerlingguy
Copy link
Owner Author

I've adjusted Appendix A to be more aware of the changes to the setup process in later versions of Windows 10, and also added a warning referencing this GitHub issue, so Windows users can follow it as I hopefully—over time—am able to figure out how to get all this book's examples running more seamlessly under WSL.

@mikeysan
Copy link

Thanks for the hard work troubleshooting and documenting your issue, @geerlingguy. I have been dealing with this same issue on version 2.2.13.
Your hard work helps me know I'm not losing it just yet.
image

@hstandeffer
Copy link

hstandeffer commented Feb 10, 2021

Has there been any update on this? I've tried everything and just can't get past the point where I run either Vagrant up or provision. I've gotten many different errors, from getting stuck on "SSH auth method private key", problems with ansible connecting to localhost via ssh. It's obviously a problem with the SSH key, but I'm truly running out of things to try.

I even temporarily gave up on the Windows solution and installed Ubuntu on a VM to only discover my computer didn't support virtualization for nested VMs... so now I'm back to hoping this will work someday.

I appreciate all of the hard work you've done @geerlingguy despite not hearing from the actual team at Vagrant.

Edit: Here's the logs of what I get when running vagrant up when ansible can't connect:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/ubuntu-20.04' version '202012.23.0' is up to date...
==> default: [vagrant-hostmanager:guests] Updating hosts file on active guest virtual machines...
==> default: [vagrant-hostmanager:host] Updating hosts file on your workstation (password may be required)...
[sudo] password for ___:
==> default: Running provisioner: ansible...
    default: Running ansible-galaxy...
- changing role composer from 1.9.0 to 1.9.0
- downloading role 'composer', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-composer/archive/1.9.0.tar.gz
- extracting composer to /mnt/c/test/trellis/vendor/roles/composer
- composer (1.9.0) was installed successfully
- changing role ntp from 2.2.0 to 2.2.0
- downloading role 'ntp', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-ntp/archive/2.2.0.tar.gz
- extracting ntp to /mnt/c/test/trellis/vendor/roles/ntp
- ntp (2.2.0) was installed successfully
- changing role logrotate from v0.0.5 to v0.0.5
- downloading role 'logrotate', owned by nickhammond
- downloading role from https://github.com/nickhammond/ansible-logrotate/archive/v0.0.5.tar.gz
- extracting logrotate to /mnt/c/test/trellis/vendor/roles/logrotate
- logrotate (v0.0.5) was installed successfully
- changing role swapfile from v2.0.32 to v2.0.32
- downloading role 'swapfile', owned by oefenweb
- downloading role from https://github.com/Oefenweb/ansible-swapfile/archive/v2.0.32.tar.gz
- extracting swapfile to /mnt/c/test/trellis/vendor/roles/swapfile
- swapfile (v2.0.32) was installed successfully
- changing role mailhog from 2.2.0 to 2.2.0
- downloading role 'mailhog', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-mailhog/archive/2.2.0.tar.gz
- extracting mailhog to /mnt/c/test/trellis/vendor/roles/mailhog
- mailhog (2.2.0) was installed successfully
 [WARNING]: - dependency geerlingguy.daemonize from role mailhog differs from
already installed version (1.2.1), skipping

    default: Running ansible-playbook...

PLAY [Set ansible_python_interpreter] ******************************************

TASK [python_interpreter : Get Ubuntu version] *********************************
System info:
  Ansible 2.8.0; Vagrant 2.2.14; Linux
  Trellis version (per changelog): "Use Python 3 for `ansible_local` Vagrant provisioner"
---------------------------------------------------
Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port
2222: Connection refused
fatal: [default]: UNREACHABLE! => {"changed": false, "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.```

@jkrooncoding
Copy link

Has there been any update on this? I've tried everything and just can't get past the point where I run either Vagrant up or provision. I've gotten many different errors, from getting stuck on "SSH auth method private key", problems with ansible connecting to localhost via ssh. It's obviously a problem with the SSH key, but I'm truly running out of things to try.

I even temporarily gave up on the Windows solution and installed Ubuntu on a VM to only discover my computer didn't support virtualization for nested VMs... so now I'm back to hoping this will work someday.

I appreciate all of the hard work you've done @geerlingguy despite not hearing from the actual team at Vagrant.

Edit: Here's the logs of what I get when running vagrant up when ansible can't connect:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/ubuntu-20.04' version '202012.23.0' is up to date...
==> default: [vagrant-hostmanager:guests] Updating hosts file on active guest virtual machines...
==> default: [vagrant-hostmanager:host] Updating hosts file on your workstation (password may be required)...
[sudo] password for ___:
==> default: Running provisioner: ansible...
    default: Running ansible-galaxy...
- changing role composer from 1.9.0 to 1.9.0
- downloading role 'composer', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-composer/archive/1.9.0.tar.gz
- extracting composer to /mnt/c/test/trellis/vendor/roles/composer
- composer (1.9.0) was installed successfully
- changing role ntp from 2.2.0 to 2.2.0
- downloading role 'ntp', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-ntp/archive/2.2.0.tar.gz
- extracting ntp to /mnt/c/test/trellis/vendor/roles/ntp
- ntp (2.2.0) was installed successfully
- changing role logrotate from v0.0.5 to v0.0.5
- downloading role 'logrotate', owned by nickhammond
- downloading role from https://github.com/nickhammond/ansible-logrotate/archive/v0.0.5.tar.gz
- extracting logrotate to /mnt/c/test/trellis/vendor/roles/logrotate
- logrotate (v0.0.5) was installed successfully
- changing role swapfile from v2.0.32 to v2.0.32
- downloading role 'swapfile', owned by oefenweb
- downloading role from https://github.com/Oefenweb/ansible-swapfile/archive/v2.0.32.tar.gz
- extracting swapfile to /mnt/c/test/trellis/vendor/roles/swapfile
- swapfile (v2.0.32) was installed successfully
- changing role mailhog from 2.2.0 to 2.2.0
- downloading role 'mailhog', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-mailhog/archive/2.2.0.tar.gz
- extracting mailhog to /mnt/c/test/trellis/vendor/roles/mailhog
- mailhog (2.2.0) was installed successfully
 [WARNING]: - dependency geerlingguy.daemonize from role mailhog differs from
already installed version (1.2.1), skipping

    default: Running ansible-playbook...

PLAY [Set ansible_python_interpreter] ******************************************

TASK [python_interpreter : Get Ubuntu version] *********************************
System info:
  Ansible 2.8.0; Vagrant 2.2.14; Linux
  Trellis version (per changelog): "Use Python 3 for `ansible_local` Vagrant provisioner"
---------------------------------------------------
Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port
2222: Connection refused
fatal: [default]: UNREACHABLE! => {"changed": false, "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.```

I would also like to know if there's any updates. Just bought the Ansible for Devops book, trying to follow along with my Windows machine but I can't get through Chapter 2. Tried installing through pip and the official installation for Ubuntu through Ansible's docs, but nothing is working.

@dglinder
Copy link
Contributor

dglinder commented Feb 11, 2021 via email

@Xtravaganz
Copy link

Xtravaganz commented Mar 10, 2021

that's my system setting
grafik

My solution was to edit sshd config, service sshd restart and service sshd status. because default port was 22 on the VM.

After vagrant up, all was working fine.

@Xtravaganz
Copy link

Adding v.gui = true in the virtualbox config in the Vagrantfile allows me to log into the machine manually, though if I do that and check things out, sshd is running, but there's definitely not a private host network started up on 192.168.88.8... so I'm not sure what's going wrong with Vagrant/VirtualBox networking here. (Also, screen is teensy tiny.)

Screen Shot 2020-06-23 at 10 49 43 PM

grafik
So you can make it bigger ;)

@LoLo2207
Copy link

LoLo2207 commented Mar 11, 2021

Hi, I've just started with the book and this was my first issue. I fixed it changing the host IP in the Vagrantfile

config.ssh.host = '192.168.1.22'

After that, I have no more Connection Refused. Retrying... and vagrant ssh works too!

@ozboss
Copy link

ozboss commented Apr 7, 2021

This took me some time to figure out, but here is my complete solution:

  1. Install WSL2, and install Ubuntu in WSL2.

  2. Install VirtualBox (I'm running 6.1.x).

  3. Inside WSL2/Ubuntu/Bash environment, install Vagrant:
    a. wget https://releases.hashicorp.com/vagrant/2.2.15/vagrant_2.2.15_linux_amd64.zip
    b. unzip vagrant_2.2.15_linux_amd64.zip
    c. sudo mv ./vagrant /usr/local/bin/vagrant
    d. nano ~/.profile (~/.bash_profile if exists) and add:

    export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
    export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox"
    

    e. Restart shell
    f. vagrant --version

  4. Check if "bsdtar" is installed: bsdtar --version (to install on ubuntu: sudo apt install libarchive-tools)

  5. Fix vagrant for WSL2:
    a. Check this thread or this for a fix
    b. Otherwise install "virtualbox_WSL2" plugin: vagrant plugin install virtualbox_WSL2 (source)

  6. Enable chmod (required for vagrant ssh, enables correct permissions for private key)
    a. Edit or create /etc/wsl.conf
    b. add:

    [automount]
    options = "metadata"
    

    c. Shutdown WSL from powershell: wsl --shutdown

  7. Create vm:
    a. vagrant box add geerlingguy/centos7
    b. vagrant init geerlingguy/centos7
    c. vagrant up
    d. Make sure to allow access for both private and public networks for VirtualBox when promted by Windows Firewall

  8. SSH into vm: vagrant ssh
    (should not be asking for a password, otherwise check that the "private_key" (see vagrant ssh-config -> "IdentitiyFile") has correct permissions of "-rw-------" / "chmod 600", if not repeat step 6. and recreate vm)

Repository owner deleted a comment from snahtajain May 20, 2021
@tanuj-05
Copy link

Thanks for the hard work troubleshooting and documenting your issue, @geerlingguy. I have been dealing with this same issue on version 2.2.13.
Your hard work helps me know I'm not losing it just yet.
image

I have the same exact issue . Have been tearing my hair for the past few hours ://

@dglinder
Copy link
Contributor

I mentioned this back in February, but it's worth mentioning it again - ensure your Windows firewall is still turned off. I have had problems with Microsoft re-enabling the Firewall after patches even though I know I disabled it in the settings just a couple days before.

The build script I ended up using had the "disable Windows Firewall" portion at the beginning, and an "enable Windows Firewall" at the end just so I was slightly more protected.

@MalcolmAnderson
Copy link

I ran into the
default: Warning: Connection refused. Retrying...
issue also.
I fixed it by making sure that my vagrant project directory is sitting in my ~ dir.
I was also able to fix this by moving the .vagrant file to your ~ dir, and then symlinking it
I've got a blog post in the works, but the fix seems to be use the following folder structure for the vagrant projects
~/VMs/a4do_c03
where a4do is where my work in chapter 3 is placed.
I've had no problems with it.
In fact the first time I got it to run and then ran
vagrant ssl
and found myself immediately at the terminal for the box I did a little dance. 16 year old daughter was not amused. :)

@MalcolmAnderson
Copy link

MalcolmAnderson commented Sep 24, 2021

[Severely edited]
Ok, this is a hack.
It's only been tested with the drupal, and nodejs examples.

In each case, I added the line to the Vgrantfile that Jeff mentioned
config.vm.synced_folder '.', '/vagrant', disabled: true
but no other changes.

Here's the hack, I'm running my Ubuntu distro in WSL version 1.

To be clear, I have wsl 2 installed, but I'm telling wsl 2 to run Ubuntu as wsl 1.

I didn't come up with on my own, it was the result of 3 - 5 hours of research last week.
I'm still not 100% sure it's going to work in the book, but I'm just happy that I can vagrant up the samples.

Here's the instructions on how to change the runnnig WSL version.
HUGE SIDE NOTE: You CAN run --set-version on a running distro, it just takes lots of minutes.
Taking the time to shut the distro down before changing the wsl version made the entire process take less than 2 minutes to switch it over.

Instructions:

Open a command prompt as administrator
Run the following to get a list of distros running on WSL
wsl --list --verbose (wsl -l -v)
(Mine is named Ubuntu)

Shut down the distro you want to run vagrant on
wsl --terminate <distro name> (wsl -t)

Change the wsl version on an existing shell
wsl --set-version <distro name> <version number>

On my machine this looked like
wsl --set-version Ubuntu 1

Start a new wsl window and try it out.

I would love feedback if this ugly horrible hack worked for you.
Also I will update this next week when I've had some time to stress test this solution a little more.

@mfrieling
Copy link

Is there any update on this issue or maybe an easy solution to use Vagrant with WSL 2 without VirtualBox?

I have a complex setup with WSL 2, Docker etc. and trying the different suggestions don't work. WSL 1 doesn't work for me either because then I would have to split a project into multiple parts or duplicate it because the working environment depends on WSL 2. Thanks.

@jshank
Copy link

jshank commented Jan 13, 2022

I've just gotten this working. WSL2, Vagrant 2.2.19 using @ozboss process.

Nothing special in the Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
end

The fixes were:

@mfrieling
Copy link

Thanks @jshank. It works.

@vPeteWalker
Copy link

Software to unzip the file isn't mentioned, so running the "unzip" command will fail. I ran this command to install it within WSL2:

sudo apt-get install zip unzip -y

@vPeteWalker
Copy link

I'm unable to get 2.2.19 installed successfully. Regardless of whether I download the file from Hashicorp's website, or install it via apt-get, I am left with:

Installed Version: 2.2.9
Latest Version: 2.2.19

@TrisBits
Copy link

For anyone that wants an alternative to messing with WSL, I found the following to be a very nice workaround, to work through the book material on Windows.

@Trailingslashes
Copy link

Trailingslashes commented Feb 22, 2023

I think... unfortunately, I'm going to have to give up on this for now at least. I was so close. Vagrant builds the VM, I can see it's running and booted, but it doesn't seem like a private IP / host-only network is configured correctly (e.g. the drupal example sets up 192.168.88.8, but that IP didn't show as reachable when I booted up that VM). And whether I used port 2222 or 4222 SSH could never connect (either via vagrant ssh or via ssh -p).

Hey @geerlingguy, I was able to get around this by installing the virtualbox_WSL2 vagrant plugin:
vagrant plugin install virtualbox_WSL2

@Billy-Bogardus
Copy link

Billy-Bogardus commented Jul 1, 2023

Update

don't do this I broke ansible while doing this.

This link looks promising: https://devblogs.microsoft.com/commandline/integrate-linux-commands-into-windows-with-powershell-and-the-windows-subsystem-for-linux/
however it doesn't seem to be working for ansible i get the following error
/bin/bash: line 1: ansible: command not found

I can run nano from inside windows though

Update:
after installing ansible as root that worked - noting that this is not recomened

sudo su

pip3 install ansible

@cindrmon
Copy link

I think... unfortunately, I'm going to have to give up on this for now at least. I was so close. Vagrant builds the VM, I can see it's running and booted, but it doesn't seem like a private IP / host-only network is configured correctly (e.g. the drupal example sets up 192.168.88.8, but that IP didn't show as reachable when I booted up that VM). And whether I used port 2222 or 4222 SSH could never connect (either via vagrant ssh or via ssh -p).

Hey @geerlingguy, I was able to get around this by installing the virtualbox_WSL2 vagrant plugin: vagrant plugin install virtualbox_WSL2

yes. i agree with his solution.

for me, i tried installing the virtualbox_WSL2 vagrant plugin for both inside WSL and PowerShell and restarted my pc, and it worked!

@slavid
Copy link

slavid commented Jul 12, 2024

Since Windows 11 version 22H2, WSL2 can use the setting networkingMode to mirrored source 1, source 2 which allows things like:

  • IPv6 support
  • Connect to Windows servers from within Linux using the localhost address 127.0.0.1. IPv6 localhost address ::1 is not supported
  • Improved networking compatibility for VPNs
  • Multicast support
  • Connect to WSL directly from your local area network (LAN)

I was wondering if anyone has tried this new setting and if it fixes all the issues around wsl2 + vagrant + ansible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests