homeblogautomating puppet cron jobs and scheduled tasks

Automating with Puppet: Cron jobs and scheduled tasks

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:

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:

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:

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