Shortening .ssh/config file with patterns and multiple hosts

Recently, I’ve been digging into Sup which is a very simple deployment tool written in Go. It allows to declare named clusters of servers (called networks), Bash commands to be performed and sequences of commands (targets) in a YAML config file so that you can run things like:

sup production deploy
# or
sup production rollback

Sup is a very handy tool for deployment of applications which don’t require a lot of setup which Go applications tend to be. It can’t measure up to tools like Capistrano in complexity and expressiveness (you still write Bash) but if you just need to upload a binary file to multiple servers in parallel, tell a process monitor to restart it, and ping a Slack channel, Sup is for you.

However, there are still things to improve and I’ve been diving into Sup’s source code to do so. In the process, I came across parsing of SSH config files and by accident found out that I’ve been way too verbose with mine! So if you happen to have multiple similar Host blocks there is a chance you might shorten your config.

Patterns

First chance to save on some typing is to use patterns to describe a group of hosts at once, so that:

Host server1.example.com
  User busybee
  IdentityFile busybee_key

Host server2.example.com
  User busybee
  IdentityFile busybee_key

Host server3.example.com
  User busybee
  IdentityFile busybee_key

can become:

Host server?.example.com
  User busybee
  IdentityFile busybee_key

The server numbers are replaced by ? which serves as a wildcard for one character. Alternatively, you can use * which stands for zero or more characters. It’s far from regular expressions but even these two wildcards can simplify your config.

The obvious disadvantage is that you can’t use short aliases in tandem with patterns.

Multiple hosts

The second way to shorten your config is to list multiple hosts in the Host declaration:

Host cz.example.com
  User busybee
  IdentityFile busybee_key
  
Host sk.example.com
  User busybee
  IdentityFile busybee_key

can turn into more concise:

Host cz.example.com sk.example.com
  User busybee
  IdentityFile busybee_key

Patterns and multiple hosts can be combined into one Host block if you find use for it.

We're looking for developers to help us save energy

If you're interested in what we do and you would like to help us save energy, drop us a line at jobs@enectiva.cz.

comments powered by Disqus