about

writing

contact

Creating Configuration Files With Ruby Templates

By eric

2010-06-30

I recently had a very repetitive configuration file that needed creating. There were approximately 50 config blocks of 10 lines each with only the host name changing with each block. So I decided to take a shortcut and do it in Ruby using ERB templates. This is so easy and literally save me hours worth of work.
I started out by creating a template for the rsyslog block I wanted to replicate. In the full version of the script, there is an additional section for the access.log file which you’ll see below. If you know Rails, then this template style should look very familiar.

ERB is very useful for building all sorts of files. It does have one issue however. It doesn't handle new lines very well. When indention is important this can be a real pain. In fact, I'd go so far as to say there is a bug in ERB.