Provisioned

In the last article we had learn how to set up a Vagrant environment article, we demonstrated how to install and configure a Vagrant environment. In this article, we will aim to demonstrate executing basic shell commands, running shell scripts contained in a Vagrant file and using external scripts to provision.

Using a Vagrant file to create an operating system is a very basic way of using Vagrant. To benefit from the powerful features, we can use it to specify operating parameters and environments in a way that promotes repeatability. The simplest approach to provisioning is executing commands at the provided terminals, for example bash and sh that are part of Linux distributions. Each approach that is used for installation and software configuration is known as a provisioner.

We will begin by discussing the shell provisioner. Using the shell to provision is appropriate for two types of users. The first group of users are those who would not like to spend much time setting up an environment. The second group of users is who have not mastered the use of tools such as Chef or Puppet. When using the shell provisioner you can either use inline or path option. In the inline option, you include the commands to be executed while in the path option you specify a shell script path that will be uploaded and executed. You can specify a local or remote path.

In the learn how to set up a Vagrant development environment, we demonstrated how to set up a local environment. In this article, we will demonstrate how to set up Vagrant on a Google compute engine (GCE) and use the cloud set up.

Login to your GCE console here https://cloud.google.com/ and create a project and note down project ID. If you have not enabled Google Compute Engine API navigate to API manager, then library, search for the API and enable it.

create proj

After creating your project, create an instance that fits your needs
instance

Navigate to credentials subsection found under API manager and create a service account key. We will use the key for authorization of API requests. Download and save the key.
create key

After creating your key, click on the manage services account and copy the service account ID which ends in gserviceaccount.com. We will need the key and ID to properly configure our Google plugin.
copy key

To connect to our instance, we will use SSH so we need to create key pairs. For simplicity, we will use gcloud command tool and specify the name of the instance we would like to connect to. To get the command to connect to your instance, on your instance click on SSH then view gcloud command.

ssh

Before we can install Vagrant on our instance, we need to ensure our packages are up to date and we have installed Virtualbox and dpkg. The commands below are used to satisfy these prerequisites.

To install vagrant on our instance, we use the commands below. After installing, execute the vagrant command to confirm installation was successful
wget https://releases.hashicorp.com/vagrant/1.9.3/vagrant_1.9.3_x86_64.deb

vag install

To complete our set up, we need to install missing kernel headers and reconfigure DKMS
reconfig

We are now ready to add boxes that we can use in our environments. To add precise32 box, we use the command below.

add box

We need to create a vagrant environment. Create a directory, navigate into it and initialize it.

init

The initialization process will place a vagrantfile in our directory that we can edit to customize our environment. For example, let us use the box we added earlier. Open Vagrant file using this command nano Vagrantfile and change box configuration to what is shown below then save.

box nano

After customizing your environment via vagrantfile, you can deploy the environment and SSH into it.

start

Let us provision an environment running an ubuntu box and using inline provisioning. Add the ubuntu box using the command below.

add ub

Let us create a directory on our guest environment and add a vagrant file with the contents below. Comments help you understand the vagrant file.

Create provision/install.ssh and add the contents below that will install required software on our guest machines

In this post, we noted how provisioning in Vagrant is very flexible. You can use Shell provisioning or an external tool such as Puppet. We demonstrated how to set up a cloud environment. Finally, we demonstrated how we can use shell and external scripting.

Learn How A Vagrant Environment Is Provisioned was last modified: by