Writing puppet manifests that can remove resources as well as adding them
I use puppet to manage the configuration of the machines I manage. So far, I've been rolling out new resources to machines but recently I've wanted to remove resources from machines. Here's how I modified my cron classes so I could remove cron jobs as well as create them.
When I first wrote my cron classes, I created a separate class for each cron job, eg.
class app::queue_processor::cron::cacheload {
include
user::base
cron { cacheload:
command => '/usr/bin/php /app/queue_processor/utils/cacheload.php account > /dev/null 2>&1',
hour => '0',
minute => '0',
user => 'app',
require => User['app']
}
}
I then created another class for each profile which includes all the required cron jobs, eg:
class profile::partition::cron {
include
app::queue_processor::cron::cacheload
app::queue_processor::cron::send_move_backlog
}
This all works fine and the cron jobs are installed as required. However, I recently needed to remove the cacheload cron job from all machines.
This is simply a matter of specifying "ensure => absent" to the cron type. I therefore needed some way of passing this to the cron job classes.
I did this by creating a higher-level cron class, app::queue_processor::cron containing a definition app::queue_processor::cron::job which takes an ensure parameter that defaults to "present" if not specified. It looks like this:
class app::queue_processor::cron {}
define app::queue_processor::cron::job($ensure = present) {
include user::base
case $name {
cacheload: {
cron { $name:
command => '/usr/bin/php /app/queue_processor/utils/cacheload.php account > /dev/null 2>&1',
hour => '0',
minute => '0',
user => 'app',
require => User['app']
ensure => $ensure
}
}
send_move_backlog: {
cron { $name:
# similar cron definition here
}
}
default: { fail("Unknown queue_processor cron job type: $name") }
}
}
I use this new define in the profile::partition::cron class like this:
class profile::partition::cron {
include app::queue_processor::cron
app::queue_processor::cron::job{ send_move_backlog: }
app::queue_processor::cron::job{ cacheload: ensure => absent }
}