Hosttag - Tagging for Hosts

When you have more than a handful of hosts on your network, you need to start keeping track of what services are living where, what roles particular servers have, etc. This can be documentation-based (say on a wiki, or offline), or it can be implicit in a configuration management system. Old-school sysadmins often used dns TXT records for these kind of notes, on the basis that it was easy to look them up from the command line from anywhere.

I've been experimenting with the idea of using lightweight tags attached to hostnames for this kind of data, and it's been working really nicely. Hosttag is just a couple of ruby command line utilities, one (hosttag or ht) for doing tag or host lookups, and one (htset/htdel) for doing adds and deletes. Both are network based, so you can do lookups from wherever you are, rather than having to go to somewhere centralised.

Hosttag uses a redis server to store the hostname-tag and tag-hostname mappings as redis sets, which makes queries lightning fast, and setup straightforward.

So let's see it in action (rpms available in my yum repo):

# Installation - first install redis somewhere, and setup a 'hosttag'
# dns alias to the redis host (or use the `-s <server>` option in
# the examples that follow). e.g. on CentOS:
$ yum install redis rubygem-redis

# Install hosttag as an rpm package (from my yum repo).
# Also requires/installs the redis rubygem.
$ yum install hosttag
# gem version coming soon (gem install hosttag)

# Setup some test data (sudo is required for setting and deleting)
# Usage: htset --tag <host> <tag1> <tag2> <tag3> ...
$ sudo htset --tag server1 dns dell ldap server centos centos5 i386 syd
$ sudo htset --tag server2 dns dell ldap server debian debian6 x86_64 mel
$ sudo htset --tag server3 hp nfs server centos centos6 x86_64 syd
$ sudo htset --tag lappy laptop ubuntu maverick i386 syd

# Now run some queries
# Query by tag
$ ht dns
server1 server2
$ ht i386
lappy server1

# Query by host
$ ht server2
debian debian6 dell dns ldap mel server x86_64

# Multiple arguments
$ ht --or centos debian
server1 server2 server3
$ ht --and dns ldap
server1 server2

# All hosts
$ ht --all
lappy server1 server2 server3
# All tags
$ ht --all-tags
centos centos5 centos6 debian debian6 dell dns hp i386 laptop ldap \
maverick mel nfs server syd ubuntu x86_64

An obvious use case is to perform actions on multiple hosts using your ssh loop of choice e.g.

$ sshr $(ht centos) 'yum -y update'

Finally, a warning: hosttag doesn't have any security built in yet, so it should only be used on trusted networks.

Source code is on github - patches welcome :-).