Using SaltStack to automate Vagrant provisioning

This blog post is an excerpt from “SaltStack For DevOps” e-book that I have recently published (Now available for free for a limited period). It is also a part of a series of tutorials about some SysAdmin/DevOps tools. You can find my first blog post about Vagrant here: How to install and use Vagrant.

Introduction

Between development and live (production) environments, some problems may happens, most of them are due to the difference between the two environments. “Well, it works on my machine”, this is a common sentence that both Dev and Ops guys could use it as an excuse. Even if it could be true – in some cases, a role of a DevOps guy is to take this into consideration and provide the necessary techniques and technologies to solve this kind of problems.

Vagrant seems to be a good solution to implement in the pipeline – the bridge connecting Dev and Ops, since it allows developers to have an isolated development environment that could be similar to the production one.

In this part of the book, I am assuming that you have some basic knowledge about Vagrant.

If not, the following introduction will help you to be more familiar with this software but not an expert 🙂

A simple definition to start: Vagrant is a software that let you create and configure virtual environments for development purposes (may be other purposes also, this depends on the context of your product and enterprise).

Vagrant is an open-source (MIT) tool developed by Mitchell Hashimoto and John Bender. It manages virtual machines hosted in Oracle VirtualBox. It has its command-line tool and it uses Ruby (a dynamically typed object-oriented scripting language).

Why Vagrant ?

According to the official website, Vagrant is a tool for :

Developers

If you’re a developer, Vagrant will isolate dependencies and their configuration within a single disposable, consistent environment, without sacrificing any of the tools you’re used to working with (editors, browsers, debuggers, etc.). Once you or someone else creates a single Vagrantfile, you just need to vagrant up and everything is installed and configured for you to work. Other members of your team create their development environments from the same configuration, so whether you’re working on Linux, Mac OS X, or Windows, all your team members are running code in the same environment, against the same dependencies, all configured the same way. Say goodbye to “works on my machine” bugs.

Operations engineers

If you’re an operations engineer, Vagrant gives you a disposable environment and consistent workflow for developing and testing infrastructure management scripts. You can quickly test things like shell scripts, Chef cookbooks, Puppet modules, and more using local virtualization such as VirtualBox or VMware. Then, with the same configuration, you can test these scripts on remote clouds such as AWS or RackSpace with the same workflow. Ditch your custom scripts to recycle EC2 instances, stop juggling SSH prompts to various machines, and start using Vagrant to bring sanity to your life.

Designers

If you’re a designer, Vagrant will automatically set everything up that is required for that web app in order for you to focus on doing what you do best: design. Once a developer configures Vagrant, you don’t need to worry about how to get that app running ever again. No more bothering other developers to help you fix your environment so you can test designs. Just check out the code, vagrant up, and start designing.

Using Vagrant

To install and use Vagrant, first thing you need to do is installing Virtualbox:

Install Vagrant:

And the VirtualBox host kernel modules:

Create a work directory and cd into it:

Create your first Vagrant machine:

Then edit the Vagrantfile in the same directory

If you want to choose another OS/Distro or architecture, please visit this repository of Vagrant boxes.

Example:

Launch the Vagrant virtual machine:

And start using it:

Please note that in this tutorial, I am using Ubuntu/Debian. You should adapt your configurations to the OS you are using. 
Example: If you would like to install Apache in CentOs, you should change “apache” by “httpd” in the state files. You can find more information about setting up a “portable” state file in the book.

Salty Vagrant

 

One of the most important feature that Vagrant offers is it capability to interface with many automation and configuration management tools such as Chef, Puppet and of course SaltStack. You can of course use shell/bash/python scripts to provision your development machine.

In this part of “SaltStack For Devops“, we are more interested in using SaltStack standards as a provisioning tool. The combination of Salt and Vagrant allows you to create a powerful automated development environment.

You should have Vagrant installed before starting.
Then, you need to install Salt plugin:

In this example we will use Ubuntu Precise 64 as a guest virtual machine:

In this example, we are going to setup an Apache web server.

Create the following directories and files:

You workspace structure should look like this:

Let’s take a look at Vagrant configuration file.

You will need to :

  • Mount your Salt file root (synchronize salt/roots (in the host) to /srv/salt/ (in the guest). ).
  • Tell Vagrant that we are using SaltStack as a provisioning automation tool.

When we say “host” and “guest”, we are referring respectively to your machine and to Vagrant machine.

The Vagrant configuration file (Vagrantfile) will look like this:

Let’s get back to Salt.

To create the minion configuration file, you would have made like this:

In this example, you will be using Salt in its masterless mode, that’s why you need to update your minion configuration:

The Apache installation SLS file should be called by your local “top.sls” file.

Now, let’s create the Apache SLS file (salt/roots/apache2/apache2.sls):

And the configuration template (salt/roots/apache2/apache2.conf)

Note in the last configuration file, that we are using grains:

Those grains will be rendered using the following file:

Let’s see its content :

We are done with configurations.

Now bring you Vagrant machine online:

At this step, in the virtual machine, SaltStack (precisely salt-minion) will be installed and configured (minion configuration file) and state files will be copied (into /srv/salt/).

Ssh into it:

Now, you need to run SaltStack in your virtual machine in order to provision it :

After executing “state.highstate” call, Apache will be installed and configured into your Vagrant VM.

This tutorial is taken from SaltStack For DevOps book. The main idea behind this book is to provide a content of good quality about SaltStack since readers could have suggestions about ameliorating it before the last version will be released. SaltStack For DevOps content is not perfect, this is why your feedback is important 🙂

Actually, I am offering free downloads for a limited period, in order to get more readers and more feedback. So please do not hesitate to contact me ( amri.aymen AT gmail DOT com / @eon01 )

Cheers.

 

You may also like...