---
title: "Epic SaltStack Examples to Ease Up Infrastructure Management"
description: "Learn how to kick your DevOps skills up a notch. Explore through a plethora of SaltStack examples and learn to install packages and manage your servers in this tutorial!"
canonical: "https://adamtheautomator.com/saltstack-examples/"
---

# Epic SaltStack Examples to Ease Up Infrastructure Management

> Learn how to kick your DevOps skills up a notch. Explore through a plethora of SaltStack examples and learn to install packages and manage your servers in this tutorial!

Source: https://adamtheautomator.com/saltstack-examples/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Epic SaltStack Examples to Ease Up Infrastructure Management](https://adamtheautomator.com/wp-content/uploads/2022/03/Up-your-DevOps-Game-With-These-SaltStack-Examples.jpg)

# Epic SaltStack Examples to Ease Up Infrastructure Management

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)1 April 20229 min. read

Categories: [DevOps](/category/devops/)

Tags:[SaltStack](/tag/saltstack/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing and Configuring Salt Master Node](#installing-and-configuring-salt-master-node)
*   [Installing and Configuring Salt Minion Server](#installing-and-configuring-salt-minion-server)
*   [Authenticating Salt Master and Minion](#authenticating-salt-master-and-minion)
*   [Running Remote Commands to Install a Web Server](#running-remote-commands-to-install-a-web-server)
*   [Managing Minions with State Files](#managing-minions-with-state-files)
*   [Conclusion](#conclusion)

IT automation for DevOps engineers is genuinely addicting. Are you one of them? If you’re looking for a more powerful way to manage your infrastructure, [SaltStack](https://saltproject.io/) is the answer to your prayers. Overcoming forces anew can be daunting indeed. But don’t fret! This tutorial got you covered with SaltStack examples to explore!

SaltStack is an automation tool created to ease up infrastructure management. And in this tutorial, you’ll learn how to up your DevOps game by automating common tasks, such as installing software and configuring servers.

Ready? Read on and never waste time on manual tasks again!

## **Prerequisites**

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

*   Two [Ubuntu](https://adamtheautomator.com/install-ubuntu/) servers (one master node and one minion node) – This tutorial uses Ubuntu 20.04, but any Linux distribution will work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A user account with root/**[sudo privileges](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/).**

## **Installing and Configuring Salt Master Node**

Salt enables you to query and execute commands on remote nodes while maintaining remote nodes ensuring specific packages are installed and running. But before you can take advantage of Salt, you’ll have to install and configure Salt master on your master node.

1\. [SSH](https://adamtheautomator.com/openssh-windows/) into your master node server, and run the following [`apt update`](https://linuxize.com/post/how-to-use-apt-command/#updating-package-index-apt-update) command to update your package list with the latest packages.

```bash
sudo apt update -y
```

![Updating System Package List](https://adamtheautomator.com/wp-content/uploads/2022/03/image-533.png)

Updating System Package List

Related:[How to Set Up OpenSSH on a Windows Server](https://adamtheautomator.com/openssh-windows/)

2\. Next, run the following command to `install` Python since every Salt module, including the salt-master and the salt-client, rely on a Python interpreter.

> _You need to install at least Python 2.7 to use Salt._

```bash
# Adds the Deadsnakes PPA, which contains newer versions of Python, to your system.
sudo add-apt-repository ppa:deadsnakes/ppa
# Refreshes the list of packages from the newly-added repositories.
sudo apt update
# Installing python v3.7
sudo apt install python3.7 -y
```

3\. Once the installation completes, run the below command to verify the Python version installed.

```bash
python3.7 -V
```

The output below shows the Python version installed on your machine. Python 3.7.12 is installed in this example, but yours may be different.

![Checking Python Version](https://adamtheautomator.com/wp-content/uploads/2022/03/image-534.png)

Checking Python Version

4\. Run the following command to download (`curl`) the Bootstrap script (`install_salt.sh`) and run the script (`sudo sh`) to install the latest version (`-P`) of Salt. The `-M` flag specifies this installation is a master installation.

> _You can also install Salt using the APT package manager, but using the Bootstrap script (install\_salt.sh) is a more efficient way of installing Salt. The Bootstrap script downloads and installs all required dependencies and related packages automatically._

```bash
curl -L https://bootstrap.saltstack.com -o install_salt.sh && sudo sh install_salt.sh -P -M
```

![Downloading the Bootstrap Script and Installing Salt](https://adamtheautomator.com/wp-content/uploads/2022/03/image-535.png)

Downloading the Bootstrap Script and Installing Salt

![Verifying Salt’s Successful Installation ](https://adamtheautomator.com/wp-content/uploads/2022/03/image-536.png)

Verifying Salt’s Successful Installation

Related:[Learning Ubuntu Apt Get Through Examples](https://adamtheautomator.com/ubuntu-apt-get/)

5\. Open the `/etc/salt/master` file in your preferred text editor.

The _/etc/salt/master_ file is where you’ll put all configuration options for your Salt master. This file is automatically generated when you run the Bootstrap script (_install\_salth.sh_).

```bash
sudo nano /etc/salt/master
```

6\. Find the line that reads **interface: 0.0.0.0,** change it to your Salt master server’s public IP, and save the changes. Doing so secures your Salt master by allowing access only to authorized users.

> _The default configuration in the /etc/salt/master file is to listen on all interfaces, making your Salt master available to anyone on the network. This behavior is not desirable in a production environment, so you change the listening address._

![Changing the Listening Interface](https://adamtheautomator.com/wp-content/uploads/2022/03/image-537.png)

Changing the Listening Interface

7\. Lastly, run the below command to `restart` the `salt-master` service to apply the changes. The command doesn’t have an output but quietly restarts the `salt-master` service.

```bash
sudo systemctl restart salt-master
```

Related:[Controlling Systemd services with Ubuntu systemctl](https://adamtheautomator.com/ubuntu-systemctl/)

## Installing and Configuring Salt Minion Server

Now that you’ve installed and configured the Salt master, it’s time to install the Salt minion server. A Salt minion is a slave server/node configured to connect to a Salt master/node. The Salt minion will listen for commands from the master and execute them on the minion server.

Like the Salt master, you can install the Salt minion server using the same commands with minor changes.

1\. SSH into your minion node, and run the following commands to install the Salt minion.

```bash
sudo apt update -y
# Adds the Deadsnakes PPA, which contains newer versions of Python, to your system.
sudo add-apt-repository ppa:deadsnakes/ppa
# Refreshes the list of packages from the newly-added repositories.
sudo apt update
# Installing python v3.7
sudo apt install python3.7 -y
# Install the minion on the server (without the -M flag).
curl -L https://bootstrap.saltstack.com -o install_salt.sh && sudo sh install_salt.sh -P
```

![Installing the Salt minion](https://adamtheautomator.com/wp-content/uploads/2022/03/image-538.png)

Installing the Salt minion

2\. Next, open the `/etc/salt/minion` file in a text editor to configure the Salt minion.

```bash
sudo nano /etc/salt/minion
```

Find the line that reads master: salt, replace salt with your Salt master’s IP address, and save the changes. Your Salt cluster will fail to start if the minion cannot connect to the master.

![Adding the Salt Master’s IP Address](https://adamtheautomator.com/wp-content/uploads/2022/03/image-539.png)

Adding the Salt Master’s IP Address

3\. Finally, run the following command to restart the Salt minion service to apply the changes.

```bash
sudo systemctl restart salt-minion
```

## Authenticating Salt Master and Minion

You’ve installed and configured both Salt master and Salt minion, and that’s a good start. But before the Salt minion can communicate with the Salt master, you must authenticate them first.

1\. Run the [`salt-key`](https://docs.saltproject.io/en/latest/ref/cli/salt-key.html) command below on the Salt master to see a list of all the currently installed keys. The `salt-key` command lets you manage keys for the Salt master and minion.

```bash
sudo salt-key --finger-all
```

Copy and save the output to a secure place. You will need this key to authenticate the Salt minion.

![Listing Installed Keys](https://adamtheautomator.com/wp-content/uploads/2022/03/image-540.png)

Listing Installed Keys

2\. Jump to your minion node and open the `/etc/salt/minion` configuration file.

```bash
sudo nano /etc/salt/minion
```

Find the line that reads master\_finger: ‘ ‘, as shown below.

![Looking for master\_finger in the /etc/salt/minion Configuration File](https://adamtheautomator.com/wp-content/uploads/2022/03/image-541.png)

Looking for master\_finger in the /etc/salt/minion Configuration File

Paste the master.pub key you noted in step one inside the single quotes, or else the minion will not authenticate.

![Adding the master.pub Key](https://adamtheautomator.com/wp-content/uploads/2022/03/image-542.png)

Adding the master.pub Key

3\. Find the line that reads **id:** and provide your Salt minion a unique identifier like a name (**ata-minion-node**), and save the changes.

![Providing the Salt minion a Unique Identifier](https://adamtheautomator.com/wp-content/uploads/2022/03/image-543.png)

Providing the Salt minion a Unique Identifier

4\. Now run the below command to restart the Salt minion service again to apply the configuration changes.

```bash
sudo systemctl restart salt-minion
```

5\. Return to your Salt master and rerun the `salt-key` command below as you did in step one.

```bash
sudo salt-key --finger-all
```

As you see below, the key for the Salt minion (ata-minion-node) is listed as unaccepted. The key is waiting for the master node to prove its identity and accept the key at this stage.

![accept the key at this stage.](https://adamtheautomator.com/wp-content/uploads/2022/03/image-544.png)

accept the key at this stage.

6\. Run the following command on the Salt master to accept the key for the Salt minion (`ata-minion-node`). Type **y** and press **Enter** to confirm when prompted.

```bash
sudo salt-key -a ata-minion-node
```

![Accepting the Salt minion’s key](https://adamtheautomator.com/wp-content/uploads/2022/03/image-545.png)

Accepting the Salt minion’s key

7\. Rerun the salt-key command below for the last time to see the list of currently installed keys.

```bash
sudo salt-key --finger-all
```

You can see in the screenshot below that the minion key is now listed in accepted state. But you might encounter the following states other than accepted when working with keys:

*   Denied – Indicates the master node automatically rejected the key. Your minion node may have a duplicate ID, or it may have been rebuilt with new keys generated. sudo salt-key –finger-all, you must delete the old key from the master node before re-attempting to authenticate.
    
*   Rejected – Indicates the salt-key command rejected the key. Various reasons can cause a minion key to get rejected. These reasons can be but are not limited to your installation not matching the configuration on the master, or the key is for a different master node.
    

![Viewing the Minion Key as Accepted](https://adamtheautomator.com/wp-content/uploads/2022/03/image-546.png)

Viewing the Minion Key as Accepted

8\. Finally, run the `salt` command below to test that the minion can communicate with the Salt master.

```bash
sudo salt ata-minion-node test.ping 
```

The output’s True value indicates the Salt minion successfully connected and communicates with the Salt master. If the test.ping command fails, the output will be False instead.

![Testing if the Salt minion Communicates with the Salt master](https://adamtheautomator.com/wp-content/uploads/2022/03/image-547.png)

Testing if the Salt minion Communicates with the Salt master

> _If you have more than one minion node, run the sudo salt ‘\*’ test.ping command to test that the Salt master can communicate with the Salt minion._

## Running Remote Commands to Install a Web Server

Now that you’ve verified the Salt minion can communicate with the Salt master, your minion node is now ready to accept commands. Perhaps you’re eyeing to install a web server, like NGINX. If so, running `salt` commands can help!

1\. Run the below `salt` command to get a list of `disk.usage` for your minion node (`ata-minion-node`).

```bash
sudo salt ata-minion-node disk.usage 
```

![Getting a list of disk usage for the minion node,](https://adamtheautomator.com/wp-content/uploads/2022/03/image-548.png)

Getting a list of disk usage for the minion node,

2\. Next, run the command below to install the `nginx` web server on your minion node.

> _Note that you can replace the ata-minion-node with the wildcard (\*) symbol to install the nginx web server on all of your minion servers. Doing so saves you time as you don’t have to install NGINX on each server._

```bash
sudo salt ata-minion-node pkg.install nginx
```

![Installing the NGINX web server on the minion node](https://adamtheautomator.com/wp-content/uploads/2022/03/image-549.png)

Installing the NGINX web server on the minion node

3\. Now, switch to the minion server and run the below command to verify that you’ve successfully installed the `nginx` web server.

```bash
 sudo systemctl status nginx  
```

Below, you can see the NGINX web server status is active (running).

![Checking the NGINX Web Server Status](https://adamtheautomator.com/wp-content/uploads/2022/03/image-550.png)

Checking the NGINX Web Server Status

4\. Finally, open your favorite web browser and navigate to the IP address of your minion server. If the NGINX web server is perfectly running, you’ll get NGINX’s welcome page, as shown below.

![Viewing the NGINX Welcome Page](https://adamtheautomator.com/wp-content/uploads/2022/03/image-551.png)

Viewing the NGINX Welcome Page

## **Managing Minions with State Files**

You’ve previously learned how to run remote commands to install the NGINX web server on your minion server. But is it also possible to flexibly install packages with Salt? Yes, with Salt state files!

State files perform operations to manipulate regular files, special files, directories, and symlinks. But that’s not all. State files also let you manage your minion servers, such as installing packages.

1\. Run the `mkdir` command below to make the environment-based directory (`/svr/salt`) for your state files.

```bash
sudo mkdir /srv/salt
```

2\. Create a new state file in the _/srv/salt_ directory with your preferred text editor. You can name the state file as you like, but the state file is called _setup.sls_ in this tutorial.

> _An SLS file is a basic data structure called configuration management._

Populate the _setup.sls_ file with the code below, then save and close the file. The code below ensures that the php and unzip packages are installed on the minion server.

```bash
# Top-level key to group all network utilities-related configuration together.
network_utilities:
# State that the given package(s) should be installed on the minion server.
 pkg.installed:
# Tells Salt to install the given package(s) on the minion server.
   - pkgs:
     - php
     - unzip
```

3\. Now, run the `salt` command below to apply (`state.apply`) the state file (`setup`) to all of your minion servers.

```bash
 sudo salt '*' state.apply setup
```

After running the command, you’ll get the following output:

*   **Succeeded –** indicates whether the state was applied successfully (true) or not (false). In this case, the state was applied successfully to the **ata-minion-node** minion server. A new package was installed, which is the **php** package.
    
*   **changed –** Indicates the number of minions changed due to running the state file. In this case, the **unzip** package was already installed on the **ata-minion-node** minion server. As a result, the state file changed the **zip** packages to the latest version.
    
*   **Failed** – Indicates the number of minions that failed to apply the state. In this case, there was no failure as the state was applied successfully to all of the minion servers.
    
*   **Total states run** – Indicates the number of states that were run. In this case, there’s only one state run.
    
*   **Total run time –** Indicates the total amount of time taken to apply the state. In this case, **39.124** seconds.
    

![Applying the State File (setup.sls)](https://adamtheautomator.com/wp-content/uploads/2022/03/image-552.png)

Applying the State File (_setup.sls_)

4\. Finally, run each command below on your minion server to verify you’ve successfully installed the `php` and `unzip` packages using a state file.

```bash
php --version
unzip
```

Below, you can see the PHP and unzip versions installed. This output confirms that the state file (_setup.sls_) works in installing packages.

![Verifying installed PHP and unzip Packages Versions](https://adamtheautomator.com/wp-content/uploads/2022/03/image-553.png)

Verifying installed PHP and unzip Packages Versions

## Conclusion

With the SaltStack examples in this tutorial, you’ve learned how Salt can help manage your minion servers, either by running remote commands or using a Salt state file. You’re now equipped with the knowledge to manage your minion servers to your heart’s content.

DevOps engineers need cloud-based services to test quickly, prototype and deploy new applications and features in this day and age. So why not try out [salt cloud](https://docs.saltproject.io/en/latest/topics/cloud/index.html) to provision minions on popular public clouds?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fsaltstack-examples%2F&text=Epic%20SaltStack%20Examples%20to%20Ease%20Up%20Infrastructure%20Management)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fsaltstack-examples%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fsaltstack-examples%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/publisher/4065d9c85b2b81efa4f7fcb198951a15/ba17f0bdb02d3ab988d2a22cae0caed456e4fdcb8e356215804b14c1b3d34c38.jpg)

### [Force Security Scans with Azure DevOps Extends Templates](/force-security-scans-azure-devops-templates/)

Enforce Azure DevOps security scans with an extends template that fails the build and a Required template check on the service connection.

![](https://adamtheautomator.com/wp-content/uploads/publisher/4065d9c85b2b812bbb79d18090b8cf1a/5c974fd06f45af85a7518ad43dc562c6698da0193a329039cdfb527daac90167.webp)

### [Azure DevOps: Fix "Acquiring an agent from the cloud"](/acquiring-agent-from-cloud-azure-devops/)

Stuck on "Acquiring an agent from the cloud"? Check pool saturation, parallel job ceilings, VNet permissions, and blocked egress before editing YAML.

![](https://adamtheautomator.com/wp-content/uploads/publisher/3f25d9c85b2b81adaabefac0bca5a4ae/55e11157df7f9c791afbd26ff75cebe00d49fbfd6a51ec905abb1e4dab0659b1.webp)

### [Never Leave Notion Tasks Open After a GitHub Merge](/close-notion-tasks-on-pr-merge/)

Close Notion tasks when a GitHub pull request merges, verifying the HMAC signature, filtering unmerged closures, and deduping repeat events.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
