profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Tuesday, March 20, 2018

baby steps with vagrant on windows 10

I will try to keep what is this and what is that to a very brief. Vagrant allows developers to quickly provision VMs or environments which will be similar/identical to each other. Developers lose the escape catchphrase 'well it works on my machine so it should work on yours too'. Every human beings are created differently and so are every operating system, machine are configured differently. I can't even get the damn powercli 6.5 working on our VDI and we have to get newer VDI with windows 10 now, anyway.

Vagrant allows you to have a homogeneous environment across the development and production. Here are some words on why vagrant directly from the horse's mouth -> https://www.vagrantup.com/intro/index.html.
So I got excited that now we can have this vagrant on windows too.
I have windows 10. Vagrant also needs one of the virtualization application.
So install
  1. oracle virtualbox
  2. vagrant for windows
  3. babun [dont ask me why not cygwin, powershell, git, commander etc., I have tried and failed so just listen to me okay...]
  4. Enable virtualization in the BIOS
  5. Disable Hyper V if you are on windows like me. Launch windows command prompt as an administrator and run the following (a reboot is required after disabling hyper v)
    bcdedit /set hypervisorlaunchtype off
    launch 'turn on windows feature on or off ' from the control panel and make sure that the hyper V is disabled; otherwise you will face the following issue. You won't be able to boot your VMs.

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

Command: ["startvm", "df09f657-e04c-4d9a-b028-575f8465a5c8", "--type", "headless"]

Stderr: VBoxManage.exe: error: Raw-mode is unavailable courtesy of Hyper-V. (VERR_SUPDRV_NO_RAW_MODE_HYPER_V_ROOT)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

Now let us go shopping for our boxes.
Go to https://www.vagrantup.com/ and click on boxes. For our sanity we will go with the most popular one. https://app.vagrantup.com/ubuntu/boxes/trusty64
Click on the new tab
Make sure you create a new folder called vagrant/ubuntu and move to that path in babun console.
You can simply run the first command

vagrant box add ubuntu/trusty64
to add the box.

our lovely vagrant starts downloading this new ubuntu image.When you run the next command

vagrant init ubuntu/trusty64
it initializes the box. You will find a vagrantfile in the directory where you have opened this command prompt.

vagrant up

It automatically deploys this VM on your virtualbox.
Launch your virtualbox and this is what I saw...

Now You might say what you see in the pic is centos7 and what i was talking about was ubuntu. Yes I know because i want you to do something different that what i exactly did, otherwise there is no fun.
I did centos and you do ubuntu.
Now you want to ssh to you new VM. 

vagrant ssh-config
you should see

{ centos7 }  » vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
Now use the details provided above to connect to our virtual machine

{ centos7 }  » ssh vagrant@127.0.0.1 -p 2222 -i D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
ECDSA key fingerprint is SHA256:TNC9QzLbOObS4ZtiaaH19W5uALyScnh10i2FDJKixqM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:2222' (ECDSA) to the list of known hosts.
[vagrant@localhost ~]$
ssh <username>@<host> -p <port> -i <key>
So now you install a bunch of stuff here. Create the environment and pass it on to your fellow developer or colleague. Pass it on what? the 'Vagrantfile' that you see in your current working directory.
Go on and have fun.
update - 2018march3
if you run
vagrant ssh
it should ideally connect to the vm but it won't. So you run
vagrant --debug ssh
once to get connected to the VM and then onwards you can simply run
vagrant ssh
and you will be connected to the VM.