How to manage MariaDB with Puppet

Puppet is a powerful automation tool that helps administrators manage complex server setups centrally. You can use Puppet to manage MariaDB — let’s see how.

With Puppet, you describe system states that you want the Puppet master server [to enforce] on the managed nodes. If you don’t have Puppet installed and configured yet, please check the official Puppet documentation.

Before you can use Puppet to manage MariaDB, you must install a Puppet module that sets the proper repository corresponding to your operating system and version of MariaDB. For Red Hat-based distros, including CentOS, you can use the Yguenane MariaDB [repository] Puppet module. On your Puppet master, install the module with the command puppet module install yguenane/mariadbrepo, which puts the module files in the directory /etc/puppet/modules/mariadbrepo/.

The Yguenane module currently supports only Red Hat 5 and 6, CentOS 5 and 6, and any Fedora version that has the MariaDB repository, which, per the MariaDB 10.0 repository, means versions 19 and 20. If you need support for different versions or operating systems, you must edit the module. Its code is simple and straightforward, so you should be able to adapt it even if you don’t know Ruby, the programming language behind Puppet. For example, to add support for Red Hat or CentOS 7, edit the file /etc/puppet/modules/mariadbrepo/manifests/init.pp and change the $os_ver variable. Initially, it looks like this:

$os_ver = $::operatingsystemrelease ? {
    /6.[0-9]/  => '6',
    /5.[0-9]+/ => '5',
    default    => $::operatingsystemrelease,
    }

Change it to:

$os_ver = $::operatingsystemrelease ? {
    /7.[0-9]/  => '7',
    /6.[0-9]/  => '6',
    /5.[0-9]+/ => '5',
    default    => $::operatingsystemrelease,
}

You can edit other variables in the same file, such as $os, to add support for other operating systems. As long as there is an official MariaDB repository for the OS and version, you should be able to add support for it.

MariaDB installation on the Puppet nodes

Once you have the necessary Puppet module on the Puppet master, you can install MariaDB on the Puppet nodes. Let’s assume your Puppet manifests are found in the default /etc/puppet/manifests/site.pp file. The first thing you should do is distribute the MariaDB repo to the Puppet nodes that should have MariaDB installed. You can do it by adding code similar to this to site.pp, or to a separate file that you include in site.pp:

node 'host1' {
    class {'mariadbrepo':
        version => '10.0',
    }
}
node 'host2' {
    class {'mariadbrepo':
        version => '10.1',
    }
}

This tells Puppet to use the repository for MariaDB version 10.0 for host1, and the one for MariaDB 10.1 for host2. This is just an example to show that you can have different versions on different hosts; in real life it’s better to have the same MariaDB version throughout your whole environment to avoid compatibility issues.

The next time Puppet catalog runs on the nodes, the repository should be added and the file /etc/yum.repos.d/MariaDB.repo should appear.

Next, you can define MariaDB installation by adding a new Puppet class (named block of Puppet code):

class mariadb {
    package { 'MariaDB-server':
        ensure => installed,
    }
    service { 'mysql':
        ensure => running,
        enable => true,
    }
}

This class instructs the nodes to install the package MariaDB-server. On Red Hat or CentOS nodes it will have the same effect as running the command yum install MariaDB-server. Naturally, it will take care of all the dependencies for MariaDB-server.

After the package directive comes the service one. Notice that I am using the service name mysql, which MariaDB uses to ensure compatibility with MySQL, which it can replace. The service directive ensures that the MariaDB service (mysql) is running, which means that it also ensures that it is started for the first time after the installation. Also, the service is set to enabled, meaning that it will automatically start during the OS boot process.

The only thing left is to include this class in the node’s declaration. For example, let’s extend an example host1 declaration like this:

node 'host1' {
    class {'mariadbrepo':
        version => '10.0',
    }
    include mariadb
}

After the next Puppet catalog run on the node host1, MariaDB should be installed and started.

MariaDB configuration on the Puppet nodes

To managing the MariaDB environment using Puppet you will probably need to edit some configuration files. For example, if you want to set custom configuration values for the MariaDB server, you will have to edit the file /etc/my.cnf.d/server.cnf on each of the Puppet nodes. You can use Puppet itself to ensure that the configuration is centrally managed and consistent over time and across multiple nodes.

To send a custom configuration file such as /etc/my.cnf.d/server.cnf to your Puppet nodes, create the following resource declaration as part of your mariadb class in the site.pp file:

file { '/etc/my.cnf.d/server.cnf':
      ensure => file,
      mode   => 644,
      source => 'puppet:///conf_files/mariadb/server.cnf',
}

This code specifies the permissions of the file (644) and its source location. Translated, the location puppet:///conf_files/mariadb/server.cnf means /etc/puppet/conf_files/mariadb/server.cnf on the Puppet master.

The above source directive assumes you have configured the Puppet fileserver already. The configuration file /etc/puppet/fileserver.conf should contain the following code:

[conf_files]
   path /etc/puppet/conf_files
   allow *

Furthermore, you should change the service description to recognize the custom MariaDB server configuration file. For this purpose you can use the subscribe metaparameter. Here is how the complete mariadb class should look:

class mariadb {
    package {'MariaDB-server':
        ensure => installed,
    }
    service { 'mysql':
        ensure => running,
        enable => true,
        subscribe => File['/etc/my.cnf.d/server.cnf'],
    }
    file { '/etc/my.cnf.d/server.cnf':
        ensure => file,
        mode   => 644,
        source => 'puppet:///conf_files/mariadb/server.cnf',
    }
}

When you use the subscribe parameter, the MariaDB server will be restarted whenever you make changes to the server configuration file, and thus your changes will take effect immediately.

As you can see, it’s easy to install and configure MariaDB with Puppet. You can install different versions and manage configuration files centrally with just a little code and effort.