grm blog. Work is copyrighted unless otherwise stated.
2021-02-07 Sun
^

Configuration management in org-mode

Emacs can be used to configure your local and remote systems. Using org-mode with babel tangle you can export code snippets as files in a specific path. This path is defined in the #+begin_src line and can be any valid C-x c-f path, which includes /ssh:user@host:/path or /sudo:root@localpc:/path.

With this simple yet powerful feature org mode can be used for basic configuration management out of the box!

Let's start by creating a .bashrc for our user. It contains some basic bash settings according to my preferences.

#+begin_src sh :tangle ~/.bashrc
# don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth

HISTSIZE=50000
HISTFILESIZE=50000

# append to the history file, don't overwrite it
shopt -s histappend
shopt -s cmdhist

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

stty -ixon

export BROWSER="firefox"
export VISUAL='emacsclient -a "" -t'
export EDITOR="$VISUAL"

alias e='$EDITOR'

alias ls='ls --color=auto'
alias ll='ls -lah'
alias l='ls -lh'
​#+end_src

Notice the source block definition: #+begin_src sh :tangle ~/.bashrc. Here are more details about the header line.

Now let's see how to configure system resources i.e. use the root password. It's exactly the same, you just need to add a TRAMP sudo path, like so:

#+begin_src conf :tangle /sudo:root@host:/etc/pacman.conf
HoldPkg      = pacman glibc
Architecture = auto
Color
TotalDownload
CheckSpace
VerbosePkgLists

SigLevel    = Never DatabaseOptional

[core]
Include = /etc/pacman.d/mirrorlist

[extra]
Include = /etc/pacman.d/mirrorlist

[community]
Include = /etc/pacman.d/mirrorlist

[multilib]
Include = /etc/pacman.d/mirrorlist
​#+end_src

Deployment

In order deploy your configurations just C-c C-v t (org-babel-tangle) and you can even store the root password in emacs' authinfo database.

Onwards

From here on all of org mode and emacs is at your disposal. Your code blocks can export verbatim as well as execute themselves and export the results. You can leverage variables and include other org files for better organization. You can create custom elisp functions to deploy to specific machines (again using TRAMP with the /ssh: prefix).