Salt DevKit with External Formulas
Local Development with Vagrant using external Salt formulas
Every popular change configuration worth their salt has some form of modularity. Chef has cookbooks, Puppet has modules, Ansible has roles, and Salt Stack has formulas.
Salt Stack uses gitfs
to access external remote formulas and this can be easily configured locally so that you can rapidly develop code using public domain formulas or formulas of your own design.
This small guide shows how to setup a local development environment using Vagrant and configure it to use an external Salt formula.
Tool Prerequisites
These are the tools used in this guide:
- Vagrant is virtual machine automation solution setups up local development environments.
- VirtualBox (recommended) is cross-platform (win, linux, mac) that allows you to run virtual machines. Other virtual machine system can be used, such as Hyper-V for Windows 10 Pro or KVM (libvrt) on Linux. This guide uses VirtualBox as it is open source and cross platform.
- Bash Shell (recommended) is not strictly required, but the commands in this tutorial are tested and oriented toward
bash
. [Note: this is default in mac OS and popular Linux distros, or msys2 with Windows].
The Guide
In this guide we’ll use an external nginx-formula
to install a basic nginx server with default site. We’ll this run this locally with masterless Salt.
Step 1: Create Project Area
If you are using Bash, you can quickly create the directory and file structure with the following commands:
# create directory structure
mkdir -p ~/salt-devkit-formula/{config,roots/{pillar,salt}}
cd ~/salt-devkit-formula# create files
touch config/minion.yaml \
roots/{pillar,salt}/top.sls \
roots/pillar/nginx.sls \
Vagrantfile
After, you should have a file structure that looks like this:
.
├── Vagrantfile
├── config
│ └── minion.yaml
└── roots
├── pillar
│ ├── nginx.sls
│ └── top.sls
└── salt
└── top.sls
Step 2: Create Vagrant Configuration
Edit the Vagrantfile
configuration with the following content:
This is doing two essential things, running the highstate (top.sls
) from our future Salt state files, and using an alternative Minion configuration.
Before we provision our system with Salt Stack, we need to first install some prerequisites so that the gitfs
will work.
Step 2: Create Minion Configuration
We need to configure a local configuration to tell Salt Minion to use the nginx formula. Edit the config/minion.yaml
file with the following contents.
Step 3: Create Salt States
We want to create our top state for our single node and tell it to use the nginx state that should now be available from the formula. Edit the roots/salt/top.sls
file:
top.sls
Step 4: Create Salt Pillars
Similarly to top state file, we also want to tell our top pillar to use nginx values. Edit the roots/pillar/top.sls
and add the following:
top.sls
Now we can add some values to tailor how nginx will be installed. For this simple demo, we won’t do much other than tell the formula to use nginx debian repo to get nginx deb packages. Edit the roots/pillar/nginx.sls
with the following:
nginx.sls
Step 2: Create, Provision, and Test
Now we are ready to bring up the environment and provision it with using a state from an external module.
You can bring up the virtual guest and download and install Ubuntu 18.04:
vagrant up --no-provision
Once this is Ubuntu downloaded and running, we can easily download and install a Salt minion on Ubuntu, and then provision the system using Salt:
vagrant provision
Now that the system is fully provisioned, we can test it. In our Vagrantfile
configuration, we mapped the guest web port 80
to 8080
on our local host system. We can test this from the host using the port 8080
:
curl localhost:8080
Step 3: Clean Up
You can remove everything from the host with the command:
vagrant destroy
Using Python 3
Salt Stack currently (Feb 2020) by default uses Python 2, but Python 2 is deprecated as of 2020. The Vagrantfile
configuration supports Python 3, on a new system that has not been provisioned, you can use Python 3 with the following command:
vagrant up --no-provision
PYTHON=python3 vagrant provision
It is important to be able to test in either Python 2 or Python 3, so that you can attribute any bugs or other gothas related to the languages and dependent libraries.
Related Article
A while back I created a series to demonstrate using different change configuration tools with Vagrant provisioners, where one of these including using Salt Stack:
Vagrant Provisioning with SaltStack
Provisioning Virtual System using Masterless Salt Stack
medium.com
Links
Documentation
- Salt Formulas (SaltStack)
- SaltStack Formulas Documentation (ReadTheDocs)
- Vagrant Salt Provisioner
Salt Formulas
Conclusion
That is all there is to this: a brief guide on how create a local Salt Stack development environment and integrate use of external formulas.
Salt formulas increases modularity and reusability, and external formulas allow you to test formulas independently of your Salt states. You can create a pipeline for you Salt formulas where new code can get merged when tests pass.
Many public domain formulas (see Links above) do this using tools like Test Kitchen test harness combined with Inspec test scripts.
The question is, do you want to do this?
Unlike other package solutions (deb, rpm, npm, pypi, gems, etc), Salt Stack formulas don’t have rich metadata (such as version locking, dependencies, etc) and don’t have any rich artifact repository to distribute formulas. Only git or a file system can be used for formulas.
Given such limitations, maintaining several small git repositories for external formulas can get quite cumbersome. For this reason, most organizations have their formulas along with other salt code and pillars saved in a single git repository.