Provisioning and Data Sharing

In this lesson, we'll be provisioning our Virtual Machine and, also, learning about how we can share files between the host OS and the guest OS.

Provisioning

Let’s first understand what provisioning means and why we need it in the first place.

What is provisioning?

Provisioning is the process of setting up an IT infrastructure. It can also refer to the steps required to manage access to data and resources and make them available to users and systems.

Provisioning is not the same thing as configuration, but they are both steps in the deployment process. Once something has been provisioned, the next step is configuration.

The term “provisioning” means to make resources available before booting up the VM. There are many terms associated with provisioning, such as server provisioning, network provisioning, user provisioning, service provisioning, etc.

Let’s do some provisioning.

We’ll add a piece of code to our Vagrantfile. We’ll be feeding a series of commands to a variable and, then, passing the variable to a method, called config.vm.provision. It’s simple; let’s do this.

First, create a variable with $variableName as shown below.

$httpd

Second, feed a series of commands to this variable as shown below.

$httpd = <<SCRIPT
yum install httpd
systemctl start httpd
systemctl enable httpd
SCRIPT

Finally, put this block of code before your Virtual Machine config in a Vagrantfile.

It’ll look something like this:

$httpd = <<SCRIPT
yum install httpd
systemctl start httpd
systemctl enable httpd
SCRIPT
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.network "public_network"
  config.vm.synced_folder ".", "/home/vagrant/stuff"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Now, we just have to use the provision method to let Vagrant know that we are provisioning.

Add the following line to your Vagrantfile.

config.vm.provision "shell", inline: $httpd

Finally, your Vagrantfile will look something like this.

$httpd = <<SCRIPT
yum install httpd
systemctl start httpd
systemctl enable httpd
SCRIPT
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.network "public_network"
  config.vm.synced_folder ".", "/home/vagrant/stuff"
  config.vm.provision "shell", inline: $httpd
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Something to note here: if your Vagrant VM is already up, the provisioning won’t happen. You will have to force provision next time you spin up your VM using the --provision flag. Moreover, we can also use the command, vagrant provision <vm-name>.

In the example above, we’ve used the inline script, which is defined within the Vagrantfile. We can also use an external script by replacing inline with path, and, instead of passing the variable, we can pass the path to that script.

The following example will clarify this.

config.vm.provision "shell", path: "script.sh"

We can also pass the URL to the external script as shown below.

config.vm.provision "shell", path: "https://example.com/script.sh"

That’s it. You can run your Vagrant VM however you want. Whether you want to provision or not, that’s totally up to you.

Folder sync

You can also share files between guest OS and host OS. We can do this with the help of the synced_folders method.

Let’s see this method in action.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.network "public_network"
  config.vm.synced_folder ".", "/home/vagrant/stuff"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

In the Vagrantfile above, we’ve added a new piece of code.

config.vm.synced_folder ".", "/home/vagrant/stuff"

The first parameter, ".", specifies the path on the host machine, while the second parameter, "/home/vagrant/stuff", specifies the path within your Vagrant VM.

This line of code will pick all of the files or folders that are present in the current directory, ".", of the host OS and share all the content within the /home/vagrant/stuff directory of the guest OS. If the folder stuff doesn’t exist on the guest machine, it will create one.

Simple, isn’t it? Let’s move forward with multi-machine VMs.

Get hands-on with 1400+ tech skills courses.