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

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

Installing packages

Installing packages using salt is really easy. Let's say you want to install nginx - you can use the following syntax:

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 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

Now, create the directory /srv/salt and create a new file 'top.sls'. The top file is used to define groups of states for a server. Its basic syntax is this:

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

The statefiles can be in subfolders. In that case the correct name would be:

dir1.dir2.statefile

You can also use wildcards in the fqdn. Let's start by defining our webserver group and the state files. Put the following contents in your top.sls file:

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

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

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 directory /srv/salt/formulas and checkout the git repository found at https://github.com/saltstack-formulas/mysql-formula. 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 command:

salt-cloud -m /etc/salt/cloud.maps.d/example.conf
salt state.highstate '*.example.com'

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

Was this article helpful?


Next article:

How to use SaltStack to manage Nginx and MySQL (4/4)

SaltStack makes managing vhosts and other configuration options very simple - Let's start with a very basic example of managing a file with the following state file: Open the file /srv/salt/vhosts/example.com.sls /etc/nginx/sites-available/www.example.com.conf: file: - managed - source: salt://files/templates/nginx.conf - template: jinja - context: - domain: example.com /etc/nginx/sites-enabled/www.example.com.conf: file: - symlink - target: /etc/nginx/sites-available/www.example.com.conf This little snippet makes sure that the file, /etc/nginx/sites-available/www.example.com.conf - contains the rendered version of salt://files/templates/nginx.conf. So, how do we make that file?