Using Vagrant to Manage Linode Environments

Vagrant is a configuration management tool that allows users to create portable and reproducible work environments. Vagrant excels at providing consistent, easy-to-configure servers that can be used to keep development environments consistent across all users. It is easy to both launch and tear down, and can be supplied to any part of a development team that may need an environment but does not have the means to configure one themselves. Often paired with providers such as Puppet, Salt, and Chef, it offers an easy solution to keeping a consistent workflow.

Vagrant can be paired with Linode through the use of the vagrant-linode plugin to spawn and destroy Linode servers as needed. This guide will provide instructions on installing Vagrant, configuring the vagrant-linode plugin, and setting up a basic Apache server for testing.

Prerequisites

Generate an API Key. This will be used to create Linodes based upon your Vagrant profile:

Log in to the Linode Manager and select my profile to the upper right.

Select the API Keys tab:

Enter a label for your API Key and set an expiration time. Then click Create API Key.

Your API Key will be output in a green box. The key will only be shown once so be sure to record it for later use:

Install the vagrant-linode Plugin

From your workspace, create a directory for your project, and move into that directory:

1
2

mkdir ~/vagrant-linode
cd vagrant-linode

Install the plugin:

1

vagrant plugin install vagrant-linode

If using a Mac, it may request to install development tools. Select yes, then re-run the command.

From the vagrant-linode directory, create the Vagrantfile:

1

touch Vagrantfile

The Vagrantfile is used to describe, in code, the type of machine that Vagrant will create. It defines everything from the operating system to the users, to any applications that need to be initially installed to make one consistent work environment.

Configure the Vagrantfile

Open the Vagrantfile in your text editor of choice. In Ruby, define what version of Vagrant you are using. The 2 defines that it is Vagrant 1.1.0 leading up to Vagrant 2.0. 1 is any version of Vagrant below that:

~/vagrant-linode/Vagrantfile

1
2
3

Vagrant.configure('2')do|config|end

All code will take place between the Vagrant.configure and end lines.

When creating a guest machine – the sever that will be created – Vagrant will create a username, password, and private key to access the machine. The default username and password is vagrant. Define your own parameters for the username, and set the pathway to your own private key. If you have not generated a private and public key, you can do so by following the Securing Your Server guide:

Lines 6 defines the provider, and lines 7 and 8 define the box. Boxes are packages that include the basic requirements for a Vagrant environment to function. The supplied box is the linode box, created as part of the plugin. Replace the API-KEY with the key generated above.

In this instance, a 2GB Ubuntu 14.04 LTS Linode is being created in the Newark data center. The provider.label is the name that the Linode will show up as in the Linode Manager.

To see more options regarding the vagrant-linode plugin see the documentation on the plugin’s GitHub repository.

Set Up the Vagrant Box

Although the server can now be created successfully, many aspects of it still need to be configured. Shell scripts will be used to complete the steps from the Getting Started guide, and to install and configure Apache. Files will also be synced between the workstation and the Linode.

Configure the Server

Create a shell script called setup.sh to configure the Linode’s hostname, set the proper timezone, and update the server. Replace vagranttest with your chosen hostname, and EST with your timezone.

Line 6 defines the locally-hosted apache2 folder ('./apache2') and links it to the /etc/apache2 directory on the Linode. disabled: false ensures that it will sync.

Line 7 does the same with a yet-to-be-created ./webfiles directory that can be used to add any website files before booting the instance.

Create the webfiles folder in your vagrant-linode directory:

1

mkdir ~/vagrant-linode/webfiles

Add to this directory any files you want the Linode to serve over HTTP.

Boot an Instance

With the Vagrantfile configured, and scripts and files created, it’s now time to create the guest machine and check that it’s running properly.

From your workstation, boot the instance:

1

vagrant up

It will run through the installation process, sync the directories, and run the shell scripts.

Log into the newly-created Linode:

1

vagrant ssh

To ensure that Apache is running properly, check the status:

1

service apache2 status

It should output:

1

* apache2 is running

To see that the environment is accesible online, check for the IP address:

1

hostname -i

Then go to your chosen web browser and navigate to your ip address with :6789 appended to the end. You should see Apache2 Ubuntu Default Page.

If you wish to shut down or remove the Linode from your workspace you can do so through one of the following commands:

vagrant halt will power down the Linode through the shutdown mechanism. You can then run vagrant up again to power on the Linode.

vagrant destroy will remove the Linode entirely from your account, removing anything that was created during the Vagrant up process or added later to the server.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.