Difficulty: Beginner
Estimated Time: 15 minutes

Salt States

Remote execution is a big time saver, but it has some shortcomings. Most tasks you perform are a combination of many commands, tests, and operations, each with their own nuances and points of failure. Often an attempt is made to combine all of these steps into a central shell script, but these quickly get unwieldy and introduce their own headaches.

To solve this, SaltStack configuration management lets you create re-usable configuration templates, called states, that describe everything required to put a system component or application into a known configuration.

Scope

In this tutorial, you will learn how to:

  • Create a Salt State
  • Apply a Salt State

Environment

To prevent you from having to set up the prerequisites to learning, we've taken the liberty of pre-installing and configuring a basic Master-Minion configuration. You will be running the commands in this tutorial in the following environment:

Hostname Operating System Salt Role Salt Version
salt-master Ubuntu 16.04.5 LTS (Xenial Xerus) Master 2019.2.0 (Fluorine)
salt-minion Ubuntu 16.04.5 LTS (Xenial Xerus) Minion 2019.2.0 (Fluorine)

Good Job!

That's it for this scenario. If you're interested in learning more about Salt's state system, we suggest you check out the Salt System Reference for a deep dive into how states work in Salt.

Creating a Salt State

Step 1 of 2

Creating a Salt State

States are much easier to understand when you see them in action, so let’s make one. States are described using YAML, and are simple to create and read.

To demonstrate what a Salt State is capable of, we've put a state file called nettools.sls in the /srv/salt directory on our Salt Master that looks like:

install_network_packages:
  pkg.installed:
    - pkgs:
      - rsync
      - lftp
      - curl

This state ensures that the rsync, lftp, and curl packages are installed on whatever minion we'll be targeting.

You can take a look at this file on the Master by running cat /srv/salt/nettools.sls. As you can see, this is just a simple text file that can easily be modified if necessary. However, to save you some typing, we're providing a pre-built one here.

That's it! That is all there is to creating a Salt state. Let's head to the next step where we'll apply it.