ansible-for-devops icon indicating copy to clipboard operation
ansible-for-devops copied to clipboard

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

Open geerlingguy opened this issue 5 years ago • 35 comments

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 avatar Jun 24 '20 02:06 geerlingguy

Well that's odd. That weird bug is due to some strange issue (https://github.com/microsoft/terminal/issues/280#issuecomment-517870239) 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 avatar Jun 24 '20 03:06 geerlingguy

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 avatar Jun 24 '20 03:06 geerlingguy

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 avatar Jun 24 '20 03:06 geerlingguy

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 avatar Jun 24 '20 03:06 geerlingguy

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

geerlingguy avatar Jun 24 '20 03:06 geerlingguy

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 avatar Jun 24 '20 13:06 geerlingguy

Opened new upstream issue: https://github.com/hashicorp/vagrant/issues/11716

geerlingguy avatar Jun 24 '20 13:06 geerlingguy

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 avatar Jun 24 '20 14:06 geerlingguy

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 🐟 .

geerlingguy avatar Jun 24 '20 14:06 geerlingguy

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 avatar Jun 25 '20 22:06 gchazot

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...

gchazot avatar Jun 25 '20 22:06 gchazot

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 avatar Jun 26 '20 20:06 geerlingguy

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.

geerlingguy avatar Aug 03 '20 18:08 geerlingguy

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

mikeysan avatar Nov 16 '20 16:11 mikeysan

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.```

hstandeffer avatar Feb 10 '21 02:02 hstandeffer

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.

jkrooncoding avatar Feb 11 '21 08:02 jkrooncoding

The error message, "Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 2222: Connection refused" is the first place to check. Since you're running under WSL2, it could be the firewall on the Windows laptop denying connections to 127.0.0.1:2222, or the firewall in the Ubuntu WSL2 instance.

As a start, try disabling the Windows firewall as well as the Ubuntu/WSL2 instance firewall and see if the issue resolves itself.

Running without a firewall is a bad thing, so if that works you'll need to re-enable both and read up on how to open the required port(s) in each firewall.

Dan

On Thu, Feb 11, 2021 at 2:48 AM Jesse Kroon [email protected] wrote:

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 https://github.com/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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/geerlingguy/ansible-for-devops/issues/291#issuecomment-777284481, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAISQEYY6QMOHPIO55KVHMTS6OKURANCNFSM4OGHLQYQ .

--


"If you wish to make an apple pie from scratch, you must first invent the universe." -- Carl Sagan

"Quis custodiet ipsos custodes?" (Who can watch the watchmen?) -- from the Satires of Juvenal

"I do not fear computers, I fear the lack of them." -- Isaac Asimov (Author)


dglinder avatar Feb 11 '21 13:02 dglinder

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 avatar Mar 10 '21 08:03 Xtravaganz

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 ;)

Xtravaganz avatar Mar 10 '21 10:03 Xtravaganz

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!

LoLo2207 avatar Mar 11 '21 09:03 LoLo2207

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)

ozboss avatar Apr 07 '21 20:04 ozboss

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 ://

tanuj-05 avatar Sep 16 '21 18:09 tanuj-05

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.

dglinder avatar Sep 18 '21 01:09 dglinder

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 avatar Sep 19 '21 06:09 MalcolmAnderson

[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.

MalcolmAnderson avatar Sep 24 '21 07:09 MalcolmAnderson

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.

mfrieling avatar Jan 13 '22 16:01 mfrieling

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:

  • Follow https://github.com/geerlingguy/ansible-for-devops/issues/291#issuecomment-815253528. I didn't need to install a special version of vagrant, was able to use the Ubuntu install directions for the latest version in WSL2
  • Disable the Windows firewall for the vEthernet adapters. Check out the script at https://github.com/Karandash8/virtualbox_WSL2/issues/1#issue-866847150

jshank avatar Jan 13 '22 18:01 jshank

Thanks @jshank. It works.

mfrieling avatar Jan 14 '22 19:01 mfrieling

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 avatar Mar 16 '22 01:03 vPeteWalker

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

vPeteWalker avatar Mar 16 '22 02:03 vPeteWalker