service rhysd reload

Generating Config With Hashes in Puppet

Playing around with puppet today (as always) and stumbled across a sweet pattern in a couple of modules for creating config files from hashes. Particularly good for building overriding config.

So here is the template

mysql.d.cnf.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
### MANAGED BY PUPPET ###
<% @settings.sort.each do |section, content|      -%>
[<%= section %>]
<%   content.sort.each do |key, values|          -%>
<%     [values].flatten.sort.each do |value|     -%>
<%= value == false ? '#' : '' %><%= key -%><%=
         case value
         when true, false
           ''
         else
           " = #{value}"
         end
%>
<%     end                                       -%>
<%   end                                         -%>

<% end                                           -%>

and now the puppet define

override.pp
1
2
3
4
5
6
7
8
9
10
define mysql::server::config ($settings,) {

  file { "/etc/mysql/conf.d/${name}.cnf":
    ensure  => file,
    content => template('mysql/mysql.d.cnf.erb'),
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    require => Package['mysql-server'],
  }

and now the site.pp

site.pp
1
2
3
4
5
6
7
8
9
10
 mysql::server::config { 'dns_repl':
     settings => {
       'mysqld' => {
         'server-id'             => '5',
         'query_cache_limit'     => '5M',
         'query_cache_size'      => '128M',
         'replicate-do-db'       => [ 'dns', 'radius' ]
        }
     }
  }

this will create a file that looks like

/etc/mysql/conf.d/dns_repl.cnf
1
2
3
4
5
6
   [mysqld]
   server-id = 5
   query_cache_limit = 5M
   query_cache_size = 128M
   replicate-do-db = dns
   replicate-do-db = radius

So this is obviously awesome. Super flexible no need to define each varible you want to add into config. Just add each var to your node definition…

So far I have used this template style for powerdns and mysql config files but I am sure there are heaps more uses for this style of config generation.

Also first..!1 whoop

Comments