Managing cron.d with chef

I have recently been playing with the chef configuration management system. I was looking for a way to manage files in a directory such that any that were created by chef would be cleaned up again when they were no longer needed. A classic use case is the /etc/cron.d directory which may be populated by files from multiple sources. There appeared to be no established pattern for this but since chef allows the use of ruby in its recipes, I was able to construct the following. It assumes the use of the cron cookbook.

[code language=”ruby”]
cron_d ‘usercron.chef’ do
minute 0
hour 23
command ‘/bin/true’
user ‘myuser’

Dir.glob("/etc/cron.d/*.chef") do |f|
name = f.split(‘/’)[-1]
t = resources("cron_d[#{name}]")
rescue Chef::Exceptions::ResourceNotFound
cron_d name do
action :delete

Leave a Reply