How to use SaltStack to install packages (3/4)

Installing packages

In this part of this tutorial series, we’ll walk you through the process of installing packages using salt.

The syntax of packages is as follows:

nginx:
pkg.latest

Now, that is already a very simple syntax but what if you wanted to install a list of packages, all part of the same group of packages? Well, Salt has got you covered there as well. Let’s say you want a complete PHP Stack (Nginx, PHP5) - You can use the following syntax:

web:
pkg.latest:
- pkgs:
- nginx
- php5-fpm
- php5-cli
- php5-gd
- php5-mcrypt
- php5-mysql

Defining States and its Helpers

Now we are going to create the file which defines the states of a server. The file we want to create is:

/srv/salt/top.sls

Paste the following in the file:

base:
'*':
- base.base
'web*':
- base.webstack
'db*':
- base.dbstack

Its basic syntax is:

label:
'fqdn.server'
- statefile1
- statefile2
- statefile3

The state files can be in subfolders. In that case, the correct name would be:

dir1.dir2.statefile

Next, create the directory /srv/salt/base and create the file web.sls:

web:
pkg.latest:
- pkgs:
- nginx
- php5-fpm
- php5-cli
- php5-gd
- php5-mcrypt
- php5-mysql

install-php-mcrypt-fpm:
file.symlink:
- name: /etc/php5/fpm/conf.d/20-mcrypt.ini
- target: /etc/php5/mods-available/mcrypt.ini
- require:
- pkg: web

install-php-mcrypt-cli:
file.symlink:
- name: /etc/php5/cli/conf.d/20-mcrypt.ini
- target: /etc/php5/mods-available/mcrypt.ini
- require:
- pkg: web

Install

Now, MySQL is slightly more difficult. This is because MySQL has a number of prompts when installing using apt-get. Luckily, one of the Salt Formulas takes away that difficulty, so we will be using that.

To do so, create the following directory:

/srv/salt/formulas

Clone the Git repository found at https://github.com/saltstack-formulas/mysql-formula in this folder.

Next, we need to edit the file /etc/salt/master, to include this formula in its search path - Find the directive file_roots and make sure it looks like this:

file_roots:
base:
- /srv/salt/
- /srv/salt/formulas/mysql-formula

Restart the Salt Master service:

service salt-master restart

If you’ve followed the previous example, you can now run the following commands:

sudo salt-cloud -p fuga-cloud-config first-salt-minion
sudo salt state.highstate '*.example.com' first-salt-minion

You now have 3 web servers and 1 database server with the needed software!

In the next tutorial, we will show you how to make a very simple config for Nginx and MySQL.