Puppet: Ensure pre-defined services are running

Welcome to part two of my series of blog entries about comparing Cfengine 3 to Puppet. The goal is to write little task-orientated snippets for both configuration management tools and compare the results each time. The goal is to find out which piece of software is easiest to learn and use.

Today we are re-writing the Cfengine 3 code snippet which ensured that a list of pre-defined services are running.

Writing the Puppet code snippet for ensuring that those services are running

service { "apache2":
        ensure  => "running",

service { "mysql":
        ensure  => "running",

Save the code above as /etc/puppet/manifests/xenuser_org-002-ensure_processes_are_running.pp and execute it:

puppet apply xenuser_org-002-ensure_processes_are_running.pp
warning: Could not retrieve fact fqdn
notice: /Stage[main]//Service[mysql]/ensure: ensure changed 'stopped' to 'running'
notice: /Stage[main]//Service[apache2]/ensure: ensure changed 'stopped' to 'running'
notice: Finished catalog run in 1.35 seconds

On my box (Linux Minth 12), Apache2 and MySQL were both installed and not running. Applying the Puppet manifest started those services again. You might have to adjust the code for your local Linux test box.

Analyzing the Puppet manifest
There is not much to do in terms of analyzing the code snippet above. It is very short and simple. I like that.
One could argue that it would be better to define a list of services and apply the “ensure -> running” part on the list while iterating through it. However, the approach above is sufficient.

By the way, Puppet supports a notifying method which listens on changes of a specified config file of a specific service. If the config file is changed, the correct service will be restarted.

In comparison to the Cfengine 3 snippet we had to write less “code” and achieved the same result. What I liked most was that I did not have to provide the path to the init scripts.

As usual, you can download today’s Puppet manifest here.