8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Vm » Here

Vagrant : A Beginner's Guide

This article gives a very brief introduction to Vagrant. Why is it such a short article? Because Vagrant is simple and the Vagrant Documentation has all the details.

Prerequisites

You need to install the following software.

Vagrant supports other virtualisation tools, but VirtualBox is the default provider, and it works on Linux, Windows and Mac, so it's very handy as I use all three.

Boxes

Vagrant boxes are pre-built VM images. Think of them as base or gold images. They can be a bare-bones OS installation, or have a whole environment installed on them. Lots of Vagrant boxes are available online, so you can search for something that looks interesting. For the rest of this article we will assume you just want a basic Oracle Linux VM, so we will be using the "bento/oracle-7.6" box. You don't need to manually download it. Once we reference it Vagrant will download it for us, and it will be added to the list of locally downloaded boxes. You will only need to download this again if there is a newer version of the same box, or if you remove the box from your current list of available boxes.

We can list available boxes we've already have downloaded using the vagrant box list command.

C:\>vagrant box list
bento/fedora-28  (virtualbox, 201812.15.0)
bento/fedora-29  (virtualbox, 201812.15.0)
bento/oracle-6.9 (virtualbox, 201806.08.0)
bento/oracle-7.5 (virtualbox, 201808.24.0)

C:\>

We can manually download a new box using the vagrant box add command. This happens automatically when you reference a new box in a Vagrantfile, but you might want to add a box from a non-standard repository. In the following example we download the "bento/oracle-7.6" box from the default Vagrant Cloud, and the Oracle provided "ol76" box from yum.oracle.com.

C:\>vagrant box add bento/oracle-7.6 --provider virtualbox
==> box: Loading metadata for box 'bento/oracle-7.6'
    box: URL: https://vagrantcloud.com/bento/oracle-7.6
==> box: Adding box 'bento/oracle-7.6' (v201812.27.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/bento/boxes/oracle-7.6/versions/201812.27.0/providers/virtualbox.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
    box: Progress: 100% (Rate: 6237k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'bento/oracle-7.6' (v201812.27.0) for 'virtualbox'!

C:\>


C:\>vagrant box add --name ol76 https://yum.oracle.com/boxes/oraclelinux/ol76/ol76.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ol76' (v0) for provider:
    box: Downloading: https://yum.oracle.com/boxes/oraclelinux/ol76/ol76.box
    box: Progress: 100% (Rate: 715k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'ol76' (v0) for 'virtualbox'!

C:\>

We remove an old box we are no longer using with the vagrant box remove command.

C:\>vagrant box remove bento/fedora-28
Removing box 'bento/fedora-28' (v201812.15.0) with provider 'virtualbox'...

C:\>

Vagrantfile

A Vagrantfile describes a virtual machine you want to build. It's really simple to create one. Just create a new directory, switch to it and run the vagrant init command.

mkdir test1
cd test1
vagrant init

There will now be a file called "Vagrantfile" in the directory. If you open it, it looks quite big, but it's mostly comments. This is what it looks like if you remove the comments.

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

Vagrant.configure("2") do |config|
  config.vm.box = "base"
end

We want to use the "bento/oracle-7.6" box, so let's set that.

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

Vagrant.configure("2") do |config|
  config.vm.box = "bento/oracle-7.6"
end

We are now in a position to create and start the VM. Provided we are in the directory with the Vagrantfile, we can use the vagrant up command. Remember, if the "bento/oracle-7.6" box isn't already present on your machine, it will download it first. If you've downloaded it before, the existing box will be used.

C:\test1>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/oracle-7.6'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/oracle-7.6' is up to date...
==> default: Setting the name of the VM: test1_default_1546588309131_89558
==> 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: 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:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.2.22
    default: VirtualBox Version: 6.0
==> default: Mounting shared folders...
    default: /vagrant => C:/test1

C:\test1>

Once the command prompt gives you back control, check the VirtualBox interface. You will see a new VM there, with a really ugly name, which includes the directory name.

You can stop, start, restart and remove the VM using the following commands.

vagrant halt
vagrant up
vagrant reload
vagrant destroy -f

We can change some of the VM properties, including that ugly VM name, by editing the Vagrantfile.

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

Vagrant.configure("2") do |config|
  config.vm.box = "bento/oracle-7.6"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048      # Memory size in M.
    vb.cpus   = 1         # Number of vCPUs
    vb.name   = "test1"   # VM name.
    
    # Tell VirtualBox this VM is running on an SSD.
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', '0', '--nonrotational', 'on']
  end
end

If you've not already done so, destroy the VM and recreate it.

vagrant destroy -f
vagrant up

Check the VirtualBox interface and you will see the memory, vCPU and VM name are set as requested.

Before we do anything more, let's see how to connect to the VM.

Connecting to a VM

Provided we are in the directory with the Vagrantfile, we can connect to the VM using the vagrant ssh command.

vagrant ssh

We are connected to the "vagrant" user. This user has sudo privileges, so we can do any administration from here, or switch to other users including "root".

Vagrant automatically configures port forwarding, allowing us to connect to the VM from a SSH client. If we look at our startup output we will see lines like the following.

==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)

This means we can connect to the VM using the "vagrant" user with a password of "vagrant" as follows.

ssh vagrant@localhost -p 2222

The /vagrant Directory

Vagrant configures a shared folder called "/vagrant", which has access to the host directory holding the Vagrantfile. Once connected to a running VM, list the contents of the "/vagrant" directory.

[vagrant@localhost ~]$ ls /vagrant
Vagrantfile
[vagrant@localhost ~]$

Create a new directory, add a script into the directory and run it.

[vagrant@localhost ~]$ mkdir /vagrant/scripts
[vagrant@localhost ~]$ echo "echo **** hello ****" > /vagrant/scripts/my_script.sh
[vagrant@localhost ~]$ sh /vagrant/scripts/my_script.sh
**** hello ****

[vagrant@localhost ~]$ ls /vagrant
scripts  Vagrantfile
[vagrant@localhost ~]$ ls /vagrant/scripts
my_script.sh
[vagrant@localhost ~]$

Not surprisingly, you can see this directory and file on the host. We could have created them directly on the host and referenced them from inside the VM.

Running Build Scripts

Now we know we can place things in the home directory and reference them from inside the VM, it makes it really easy to create build scripts and initiate them during VM creation. We've added an extra provision section at the bottom to run the shell script we created in the previous section.

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

Vagrant.configure("2") do |config|
  config.vm.box = "bento/oracle-7.6"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048      # Memory size in M.
    vb.cpus   = 1         # Number of vCPUs
    vb.name   = "test1"   # VM name.
    
    # Tell VirtualBox this VM is running on an SSD.
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', '0', '--nonrotational', 'on']
  end

  config.vm.provision "shell", inline: <<-SHELL
    sh /vagrant/scripts/my_script.sh
  SHELL
end

This will run the script on the first creation of the VM, or if we reload the VM with the "--provision" option.

vagrant reload --provision

If you do this, you will see the following messages at the end of the startup.

==> default: Running provisioner: shell...
    default: Running: inline script
    default: **** hello ****

In this case it was just a silly script, but it could have been a build script to configure the VM and install software from a Yum repository, or accessed from the host via the "/vagrant" directory.

If you are using a Windows host, remember that the Windows CRLF can confuse Bash scripts, so make sure you dos2unix them.

Next Steps

We've just scratched the surface of Vagrant. Main thing to do now is to play!

I've created a number of Vagrant builds for Oracle Databases, RAC, Data Guard and ORDS amongst other things. You can find them on GitHub here.

These examples and the comments in the Vagrantfile will give you some ideas about adding the following to your own builds.

Here's a quick example of those.

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

# Set some variables.
var_disk1_name     = './test1_u01.vdi'
var_disk_size      = 10
var_public_ip      = '192.168.56.101'

Vagrant.configure("2") do |config|
  config.vm.box = "bento/oracle-7.6"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048      # Memory size in M.
    vb.cpus   = 1         # Number of vCPUs
    vb.name   = "test1"   # VM name.
    
    # Map additional ports available.
    config.vm.network "forwarded_port", guest: 1521, host: 1521
    config.vm.network "forwarded_port", guest: 5500, host: 5500

    # Add a new public network.
    config.vm.network "private_network", ip: var_public_ip, virtualbox__intnet: "public"
    
    # Add a new shared folder (host directory, internal mount point).
    # Relative paths are relative to the Vagrantfile directory.
    config.vm.synced_folder "../Downloads", "/vagrant_downloads"
    
    # Tell VirtualBox this VM is running on an SSD.
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', '0', '--nonrotational', var_non_rotational]
    
    # Add disk if it's not already present.
    unless File.exist?(var_disk1_name)
      vb.customize ['createhd', '--filename', var_disk1_name, '--size', var_disk_size * 1024]
    end
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--nonrotational', var_non_rotational, '--medium', var_disk1_name]
  end

  config.vm.provision "shell", inline: <<-SHELL
    sh /vagrant/scripts/my_script.sh
  SHELL
end

For more information see:

Hope this helps. Regards Tim...

Back to the Top.