Simple Configuration Management with BASH
How I provision & configure my personal servers.
TL;DR:
A BASH script of idempotent functions can take you pretty far in the config mgmt game. If considering how to manage a small fleet of servers (on the order of 10's), then cherry-picking from idem.sh might be worth your time.
Motivation
I manage larger fleets of servers for work, so the last thing I want is for managing my personal infrastructure to feel like work. Plus, the tooling used for larger fleets is overkill for smaller fleets. I stole some ideas from Bash Booster and built a library of idempotent functions as I needed them. It's been serving me well so I thought might be of use to polish it up and publish it.
How It Works
I have an infrastructure repository that contains all the bits needed for configuring my servers. It's basically a provision.sh script and a "filesystem" that is installed on the system. Here is an example snippet to help explain how it works:
DRYRUN=${DRYRUN:-YEEE}
FS="$(dirname $BASH_SOURCE[0])/fs"
PKGS=( ... )
LIBIDEM=/usr/local/lib/idem.sh
if ! test -f $LIBIDEM; then
curl -fsSL https://git.tfks.net/erik/idem.sh/raw/commit/05b032d2f9a29b73899d1a89eea581c7dd419980/idem.sh > $LIBIDEM
fi
source $LIBIDEM
function do-admin {
idem-cp $FS/etc/sshd.conf /etc/ssh/sshd_config
if test $? == $IDEM_DRIFT -a -z "$DRYRUN"; then
systemctl restart ssh
fi
idem-env-file $FS/etc/network.env /etc/tfks/network.env IFACE_WAN
idem-mkdir $HOME/backup
idem-install-pkgs $PKGS
for script in $FS/bin/*; do
idem-cp $script /usr/local/bin/$(basename $script)
done
idem-install-service tfks-config-drift
}
# allow this script to be sourced as a library so that individual do-xxx functions can be called
if test "${BASH_SOURCE[0]}" == "${0}"; then
do-admin
...
exit $EXIT
fi
Benefits
- Only one dependency: a single bash file with just a bunch of functions in it.
- The mental model is simple enough that anyone with just a bit of programming can grok it.
- No services, no long-running processes consuming memory & CPU. Use only as needed.
- Requiring the unsetting of
DRYRUNin order to perform changes is safe and useful to quickly see what bits have drifted out of sync.
Drawbacks
- Requires at least one person on the team to have basic BASH scripting skill. Shell scripting is relatively simple but can be very tricky.
- Some of the primitives require a human operator (eg idem-env-file).
- No support for enforcing negatives (eg "make sure this file or service does not exist") which can be annoying when renaming things.