Virtualbox Vagrant Mac



As mentioned in my first post, I’d like to try and build a Linux machine with Jenkins, GIT, Maven, Java and Docker.The end goal is to run Selenium WebDriver tests in dockerised containers. I have a lot to learn but I feel this is an area I need to grow my skill set.Also, as I work in test, I am also learning to use test-kitchen to execute infrastructure code.

  1. Vagrant Vs Virtualbox
  2. Virtualbox Mac Download
  3. Add Virtualbox To Vagrant
Vagrant virtualbox default machine folder

The book I am learning from is Ansible From Beginner to Pro.I did encounter a few problems whilst following the book as versions of libraries have changed but I’ve managed to get past these errors so far.

Vagrant version 2.2.14. Host operating system Mac OS 11.0.1 Big Sur on a 2020 Mac Mini with the new M1 chip and Rosetta installed to emulate an Intel chip. Guest operating system Ubuntu 18 Vagrantf. Ok, I found solution from a combination of online solutions. Because I renamed paths, and stupidly recreated a new vagrant VM, I had to reassociate the old VM with the new vagrant box configuration. First, you have to grab the UUID from the old VM. In Mac OSX (Windows path may be different) nano /VirtualBox VMs//.vbox.

This tutorial and the next few to follow have been completed on a Mac with OSX El Capitan. The steps on a Windows machine are likely to be different.

Goals

In this tutorial, we will:

  • Install VirtualBox
  • Install Vagrant
  • Install Ansible
  • Check it all works

Introduction

VirtualBox

Virtualisation allows multiple operating systems to simultaneously share your processor resources in a safe and efficient manner.VirtualBox is a provider for virtualisation and it allows your operating system to run in a special guest environment on top of your existing host operating system.In my case, the host operating system is OSX El Capitan.

Vagrant

Vagrant is an open source software for creating a portable development environment within minutes. It is useful for adevelopment team to mimic a server’s configuration locally. It can also be useful fortesting a version of Internet Explorer on a Mac for example.

Ansible

Ansible is a provisioning framework used to deploy software in a consistent manner in one or more machines.It ensures each machine is working with an identical environment.

Note:

I have used Homebrew to install the above mentioned software.Homebrew is a package managment system thatsimplifies the installation of software on the Mac operating system.

To install Homebrew, just follow the simple instructions on their website.

Step 1 - Install VirtualBox

In terminal, type in the following command

brew cask install VirtualBox

Step 2 - Install Vagrant

In terminal, type in the following command

brew cask install vagrant

Step 3 - Install Ansible

In terminal, type in the following command

brew install ansible

Step 4 - Check Installations were Successful

Type in the following commands and if the version is printed, then the installation was successful.

VBoxManage --version

vagrant --version

ansible --version

My versions respectively are:

5.2.6r120293

Vagrant 2.0.1

ansible 2.4.2.0

Step 5 - Install an Ubuntu Server with Vagrant

Before we start using Ansible, we’ll need an environment which we can use to develop our new infrastructure. This is where we use Vagrant.

a. Create a new directory which will contain all your virtual machine installations. I normally put all my projects under a directory called development.Create a directory called virtualmachines and inside this create a directory called ansible-playbook:

mkdir virtualmachines && cd virtualmachines

mkdir ansible-playbook && cd ansible-playbook

On my machine, the full path looks like this:

/Users/pateli03/development/virtualmachines/ansible-playbook

b. To create the virtual machine on which you’re going to install packages and configure with Ansible, run this command:

vagrant init ubuntu/trusty64

This will create a file called Vagrantfile. We will be editing this file in Step 6 but this is what the contents of the file should look like (but with a lot more comments):

c. Finally, get Ubuntu Trusty up and running by executing:

vagrant up

When you execute this command the first time, a few things happen. First, it will check if a box with the name ubuntu/trusty64exists on your machine. If it doesn’t, it will download it from Atlas, a platform to host and discover Vagrant boxes.It is maintained by HashiCorp, the team behind Vagrant.

This could take a few minutes as this is downloading an entire operating system.

  • Once downloaded, Vagrant uses VirtualBox to create a virtual machine and boot it.

You can check the status of this virtual machine by running the command:

vagrant status

You can also log in to the machine using:

vagrant ssh

This logs in to the machine using a known SSH key that was generated when the virtual machine was created.

To logout of this machine you can use the command exit

Step 6 - Hello Ansible

We need to let Vagrant know that we want to run Ansible on this virtual machine by adding some instructionsto the Vagrantfile. Go ahead and open the Vagrantfile in your favourite text editor but I recommend using Vim which isa text editor on Unix-like operating system. Vim has recently become important in my day-to-day life so it is another toolI would like to be more fluent in.

Open your Vagrantfile using this command:

vim Vagrantfile

Press i on your keyboard to insert text. You will see the bottom of the terminal window change to -- INSERT --

before the last end statement, add the following configuration:

It should look similar to this:

To save the changes:

press ESC on the keyboard to come back to command mode

then Shift + :

then w + q

then Enter to finish saving your changes.

(Note: Don’t enter the + symbol)

Step 7 - Creating the playbook

An Ansible playbook is a YAML file which contains a set of instructions to manage the configuration of remote machines.We will create a playbook now as using the same path specified in our Vagrantfile above – in a directory called provisioning.

mkdir provisioning && cd provisioning

touch playbook.yml

vim playbook.yml

press i on the keyboard to insert text

Copy and paste the below configuration code which will ping you virtual machine to confirm you can connect to it.

I’ll go into more detail on the configuration format in subsequent posts but be aware YAML syntax is whitespace sensitive.Tabs and whitespaces mean different things. YAML uses spaces and the suggested indentation is 2 spaces.

To save the changes:

press ESC on the keyboard to come back to command mode

then Shift + :

then w + q

then Enter to finish saving your changes.

Step 8 - Running the playbook

You should now be able to run Ansible on your machine. Change directory one level up to the location of your Vagrantfile:

cd .

vagrant provision

You should see something quite similar to the image below. Notice the ping task was successfully executed.

Boom! You’ve just provisioned your first virtual machine using Ansible and Vagrant.

Step 9 - Let’s look at VirtualBox

As mentioned earlier, VirtualBox provides us with virtualisation and although it seems like we haven’t done anything with it,let’s have a look. Open the VirtualBox application (You can easily do this with Spotlight - cmd + space and type VirtualBox)

You should have something similar to this:

This is letting us know that Ubuntu Trusty is running properly. Happy days.

Summary

VirtualBox is the software which runs the Operating System and Vagrant acts as a wrapper around VirtualBox to manage it.Together, they can be used to create a local environment that matches your production environment or an environment toexecute Selenium Tests.

With our foundations in place - the next post will focus on using test-kitchen, a test harness used for testing infrastructure codeon isolated platforms. This is quite exciting, a TDD method to design infrastructure.


Commands used in this post

CommandDescription
brew cask install VirtualBoxInstalls VirtualBox using Homebrew
brew cask install vagrantInstalls Vagrant using Homebrew
brew install ansibleInstalls Ansible using Homebrew
VBoxManage --versionVersion of VirtualBox installed
vagrant --versionVersion of Vagrant installed
ansible --versionVersion of Ansible installed
vagrant init ubuntu/trusty64Initialise a virtual machine with Ubuntu Trusty
vagrant upStart a virtual machine
vagrant statusStatus of virtual machine
vagrant sshSSH into the virtual machine
vagrant provisionRun a playbook to provision the virtual machine
vim VagrantfileOpen a file called Vagrantfile with Vim text editor
touch playbook.ymlCreate a new file called playbook.yml


Please enable JavaScript to view the comments powered by Disqus.

Virtual Machine

The virtual machine created in VirtualBox can use any configuration you would like, but Vagrant has some hard requirements:

-The first network interface (adapter 1) must be a NAT adapter. Vagrant uses this to connect the first time.

-The MAC address of the first network interface (the NAT adapter) should be noted, since you will need to put it in a Vagrantfile later as the value for config.vm.base_mac. To get this value, use the VirtualBox GUI.

Other than the above, you are free to customize the base virtual machine as you see fit.

Additional Software

In addition to the software that should be installed based on the general guide to creating base boxes, VirtualBox base boxes require some additional software.

VirtualBox Guest Additions

VirtualBox Guest Additions must be installed so that things such as shared folders can function. Installing guest additions also usually improves performance since the guest OS can make some optimizations by knowing it is running within VirtualBox.

Download facebook messenger for mac. Before installing the guest additions, you will need the linux kernel headers and the basic developer tools. On Ubuntu, you can easily install these like so:

To install via the GUI:

Next, make sure that the guest additions image is available by using the GUI and clicking on 'Devices' followed by 'Install Guest Additions'. Then mount the CD-ROM to some location. On Ubuntu, this usually looks like this:

Finally, run the shell script that matches your system to install the guest additions. For example, for Linux on x86, it is the following:

If the command succeeds, then the guest additions are now installed!

To install via the command line:

You can find the appropriate guest additions version to match your VirtualBox version by selecting the appropriate version here. The examples below use 4.3.8, which was the latest VirtualBox version at the time of writing.

If you did not install a Desktop environment when you installed the operating system, as recommended to reduce size, the install of the VirtualBox additions should warn you about the lack of OpenGL or Window System Drivers, but you can safely ignore this.

If the commands succeed, then the guest additions are now installed.

Packing VirtualBox

Vagrant includes a simple way to package VirtualBox base boxes. Once you've installed all the software you want to install, you can run this command:

Where 'my-virtual-machine' is replaced by the name of the virtual machine in VirtualBox to package as a base box.

It will take a few minutes, but after it is complete, a file 'package.box' should be in your working directory which is the new base box. At this point, you've successfully created a base box.

Vagrant Vs Virtualbox

Raw Contents

This section documents the actual raw contents of the box file. This is not as useful when creating a base box but can be useful in debugging issues if necessary.

Virtualbox Mac Download

A VirtualBox base box is an archive of the resulting files of exporting a VirtualBox virtual machine. Here is an example of what is contained in such a box:

In addition to the files from exporting a VirtualBox VM, there is the 'metadata.json' file used by Vagrant itself.

Also, there is a 'Vagrantfile.' This contains some configuration to properly set the MAC address of the NAT network device, since VirtualBox requires this to be correct in order to function properly. If you are not using vagrant package --base above, you will have to set theconfig.vm.base_mac setting in this Vagrantfile to the MAC address of the NAT device without colons.

Add Virtualbox To Vagrant

When bringing up a VirtualBox backed machine, Vagrant imports the 'box.ovf' file found in the box contents.