Over a million developers have joined DZone.

Standardizing Cron With Puppet

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.

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

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 webportal application, every night at midnight. I'm going to add this to my Puppet code so that all the database servers for my webportal 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 webportal 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.

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Topics:
puppet enterprise ,time ,cron

Published at DZone with permission of Dylan Ratcliffe, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}