Published on 6 September 2016 by

Editor's Note: This post is a taste of what you'll get in our ebook about about automating routine IT tasks with Puppet, The Top 5 Things to Automate with Puppet Right Now. Download it for free to read the rest.

Many IT organizations use Cron to run regularly scheduled tasks. Trouble is, it’s easy to forget what Cron jobs exist where, and why. Forgotten Cron jobs can wreak havoc on systems, and it's often difficult to track down the root cause — the forgotten Cron task itself.

Puppet Enterprise includes a way to manage Cron resources so you can see all the Cron jobs under management from a central place, and know exactly what they do and when they do it.

In this example, instead of creating a new Cron job or scheduled task, we are going to take one that we know is correct and make sure that it is the same across all our systems.

Let's start by checking what Cron jobs and scheduled tasks we have already:

Cron jobs

puppet resource cron

Scheduled tasks on Windows

puppet resource scheduled_task

On one of my database servers I get:

cron { 'unmanaged:su_-mysqlcheck_--databases_webportal_customer-1':
  ensure  => 'present',
  command => 'mysqlcheck --databases webportal_customer',
  hour    => ['0'],
  target  => 'root',
  user    => 'root',
}

This represents a Cron job I created manually that verifies my customer database from my web portal application, every night at midnight. I'm going to add this to my Puppet code so that all the database servers for my web portal application run the same cleanup at the same time. In order to do that, I am going to first create a role and profile for this to live in.

Now at this point, it might be tempting to create a profile called profile::cron_jobs and put all our Cron jobs in there. However, we are going to create one called profile::webportal_database instead. This is because it makes more sense to group our Puppet resources by the particular component they are relevant to than by their type. Using the knowledge we acquired in Task 1, our profile will look like this:

# site/profile/manifests/webportal_database.pp
class profile::webportal_database {

  # This code is copy-pasted from the `puppet resource cron` command
  cron { 'vacuum_customer_database':
    ensure  => 'present',
    command => 'mysqlcheck --databases webportal_customer',
    hour    => ['0'],
    target  => 'root',
    user    => 'root',
  }

}

Note that I have changed the title of this resource to be more descriptive. This is just for readability.

Now that I have created a profile, I can create a role for my web portal database server:

# site/role/manifests/webportal_database
class role::webportal_database {
  include profile::base               # From Task 1
  include profile::webportal_database # The one we just created
}

Now deploy your code (The same we did when we set up the Puppet master).

The last thing we need to do is assign our new role to our database servers. We will do this by going into the Puppet Enterprise console, clicking Nodes > Classification > Add Group… This will allow us to add a group for our database servers and assign our new role to it.

Create a group called Database Servers. Click the newly created group from the list, and under Pin specific nodes to the group, open it up, add your database servers, and click Pin. Now go to the Classes tab and add your new role. If it is not available yet, you may need to click the Refresh button in the middle right of the screen. Once you have added the role::webportal_database role, click Commit Changes.

Run Puppet on your database servers using puppet agent -t or by clicking Run Puppet in the console.

Dylan Ratcliffe is a professional services engineer at Puppet.

Learn more

Share via:
Posted in:

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.