Salt is an open source infrastructure management tool. While one use is to manage thousands of servers, it’s also very useful as a server provisioning tool when trying to deploy a web application.

Here’s a short post about using Salt (well, we’re actually using Salty Vagrant) with Vagrant to make creating fully provisioned vagrant boxes easy and quick.

First of all, I’m going to assume you already have both Saltstack, Vagrant and Salty Installed. I hate articles that are heavy on the installation instructions and sacrifice actual content. If you don’t then you can find out more about installing Saltstack here, Vagrant here and Salty Vagrant here.

Start with a simple Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
  config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  # Webserver
  config.vm.forward_port 8000, 8081

  config.vm.customize ["modifyvm", :id, "--memory", 1024]

  # SALT STACK PROVISIONING
  ## For masterless, mount your salt file root
  config.vm.share_folder "salt_file_root", "/srv", "/path/to/salt/root"
  ## Use all the defaults:
  config.vm.provision :salt do |salt|
    salt.run_highstate = true

    ## Optional Settings:
    # salt.minion_config = "salt/minion.conf"
    # salt.temp_config_dir = "/existing/folder/on/basebox/"
    # salt.salt_install_type = "git"
    # salt.salt_install_args = "develop"

    ## If you have a remote master setup, you can add
    ## your preseeded minion key
    # salt.minion_key = "salt/key/minion.pem"
    # salt.minion_pub = "salt/key/minion.pub"
  end
end

Then create your salt stack layout. Mine looks something like:

salt
├── minion.conf
└── roots
    ├── pillar
    └── salt
        ├── top.sls
        ├── vimrc
        ├── virtualenv.sls
        └── webserver.sls

It’s placed in the root directory of the project, where the Vagrantfile is. For simplicities sake I’ve not populated the pillar directory with any data. For this example I’ll just be using salt states as this works just fine for a simple vagrant deployment.

Files

minion.conf

This is where your minion configuration should go. This is a vagrant deployment so it has one line:

file_client: local

top.sls

This is an important file. sls files are SaLt State files. The top file is used by Salt in order to know what you want to do. In this example we want to run both the webserver and the virtualenv states, so we have a top file structured like so:

base:
  '*':
      - webserver
      - virtualenv

webserver.sls

This is one of our state files:

debconf-utils:
   pkg:
    - installed

nginx:
  pkg:
    - installed

apache2:
  pkg:
    - installed

python-pip:
  pkg:
    - installed

vim:
  pkg:
    - installed

/home/vagrant/.vimrc:
  file:
    - managed
    - source: salt://vimrc

That will make sure all of the required packages are installed on the server and will also copy the vimrc file into vagrants home directory.

virtualenv.sls

virtualenvwrapper:
  pip.installed

/var/virtualenv:
  virtualenv.managed:
    - no_site_packages: True

This is slightly different to the webserver example above. It’s not just installing packages and copying files. This time we’re installing virtualwrapper with pip and then building a Python virtual environment that we can use to run, for example, a Django web site.

Along side virtualenv and pip, Salt comes with lots of other modules that take a lot of the pain out of deploying specific set ups. You can find a list of them in the Salt docs. If you can’t find what you’re looking for, Salt modules are written in Python, making it very easy to extend Salts existing functionality to help you fully control deployment.

Now we have all of the files in place we can run vagrant:

vagrant up

The first run may take some time to run as it needs to build the box from scratch. Once it’s run to completion you should be able to log into the box with

vagrant ssh

All of the packages should be present and a virtual environment should be in /var/virtualenv .

That’s it!

Search

Bad implementation always trumps good ideas

19 September 2015 11:34 a.m.

Good practice in software development has always preached loose coupling and high cohesion.

While most developers understand this and try ...

Read More

New platform, new challenges

13 September 2015 19:13 p.m.

There was a question raised by a collegue of mine recently, that asked "How long should it take to get ...

Read More

SaltStack and Vagrant

14 February 2013 15:14 p.m.

Salt is an open source infrastructure management tool. While one use is to manage thousands of servers, it’s also very ...

Read More