Search code examples
puppethiera

Adding custom server options with puppetlabs mysql module


I have a puppet manifest that contains this phrase (my question is about the production block at the end), used to set up mysql configuration files:

class web_mysql_server {

  include web_mysql::packages
  include web_mysql::mysql_server
}

class web_mysql::packages {

  Package { ensure => installed }
  package { 'libmysqlclient-dev': }
}

class web_mysql::mysql_server {

# Qualified keys (dot notation: web_mysql_server.mysql_database) isn't available
  # until hiera 2.0.
  $mysql_config = hiera('web_mysql_server')

  # https://forge.puppetlabs.com/puppetlabs/mysql
  class { '::mysql::server':
root_password => '...',
remove_default_accounts => true,
override_options        => {
  # The data in this block gets written to /etc/mysql/my.cnf.
  'mysqld'     => {
    max_connections => '1024', # No good reason but parroting others.
    key_buffer_size => '512M', # No good reason but parroting others.
  },

  'production' => {
    adaptor  => 'mysql2',
    database => $mysql_config['mysql_database'],
    user     => $mysql_config['mysql_username'],
    password => $mysql_config['mysql_password'],
    host     => '0.0.0.0',
    encoding => 'UTF8',
  },

But sometimes I'd maybe like the database to be a staging database, and so it's a bit confusing to call it production. No problem, I've recently learned about hiera, it's my hammer for all nails today:

  $mysql_config['mysql_name'] => {
    adaptor  => 'mysql2',
    database => $mysql_config['mysql_database'],
    user     => $mysql_config['mysql_username'],
    password => $mysql_config['mysql_password'],
    host     => '0.0.0.0',
    encoding => 'UTF8',
  },

No, that doesn't work, though the error ("syntax error at line (...with '=>'...), expected '}'") isn't terribly revealing.

("Key" might be the wrong word. Please correct me. I'm a bit new to puppet. And that itself may be why I've not been able to answer this question by googling and reading.)

Thanks for any pointers.


Solution

  • Update: I see now that the mysql module provides an option to add section to the config file out of the box. Here is how it should be used according to the documentation:

    $override_options = {
      $mysql_config['mysql_name'] => {
        adaptor => 'mysql2',
        database => $mysql_config['mysql_database']
        # ...
      }
    }
    
    class { '::mysql::server':
      root_password           => 'strongpassword',
      remove_default_accounts => true,
      override_options        => $override_options,
    }
    

    Or you can save some code if you put the mysql_name value under a different hiera key:

    $override_options = {
      hiera('mysql_name') => hiera('web_mysql_server')
    }
    

    Original answer:

    If your actual intention is just to create a configuration file, the best way is using templates in erb syntax.

    Here is an example. First, you put a my.cnf.erb file to the templates directory of your module (e.g. modules/web_mysql/templates):

    [<%= @mysql_config['mysql_name'] %>]
    adaptor =  mysql2
    database = <%= @mysql_config['mysql_database'] %>
    user     = <%= @mysql_config['mysql_username'] %>
    password = <%= @mysql_config['mysql_password'] %>
    host     = 0.0.0.0
    encoding = UTF8
    

    You can use this template in your web_mysql::mysql_server class to create the config file:

    class web_mysql::mysql_server {
      # ...
      $mysql_config = hiera('web_mysql_server')
      file { '/etc/mysql/my.cnf':
        ensure  => 'present',
        content => template('web_mysql/my.cnf.erb'),
      }
    }