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.
1 2 3 4 5 6 7 8 9 10 11 | rsyslog_block = %{# # < %= virtual_host %> # $InputFileName /var/log/httpd/< %= virtual_host %>-error.log $InputFileTag < %= virtual_host %>_error $InputFileStateFile stat-< %= virtual_host %>-error.log $InputFileSeverity info $InputFileFacility local5 $InputRunFileMonitor } |
The below block of code creates a configuration block (above) for each of virtual hosts in the array and then prints it to the screen.
1 2 3 4 | %w{ vhost.com bar.myhost.com }.each { |virtual_host| puts ERB.new( rsyslog_block ).result(binding) } |
This is going to produce the following block which you can then pipe to a file:
1 2 3 4 5 6 7 8 9 10 | ...Removed for brevity... # # bar.myhost.com # $InputFileName /var/log/httpd/bar.myhost.com-error.log $InputFileTag bar.myhost.com_error $InputFileStateFile stat-bar.myhost.com-error.log $InputFileSeverity info $InputFileFacility local5 $InputRunFileMonitor |
Here is the script in its entirety:
The post Creating Configuration Files With Ruby Templates appeared first on Live. Interesting. Stories.