A collection of task oriented solutions in Puppet

 

Manage logrotate jobs

Challenge

You want to add log rotation

Solution

Puppet doesn't provide a native way to manage logrotate configuration so you'll need to install a module to manage it.

puppet module install puppet-logrotate

...
Notice: Installing -- do not interrupt ...
/etc/puppetlabs/code/environments/production/modules
|--- puppet-logrotate (v3.2.1)
  |--- puppetlabs-stdlib (v4.25.1)
...

You can then add new rotations using logrotate::rule resources.

class rotate {

  logrotate::rule { 'backups':
    path         => '/var/log/backups',
    rotate       => 2,
    rotate_every => 'week',
    compress     => true,
    dateext      => true,
  }

}
$ cat /etc/logrotate.d/backups

# THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET.  ANY CHANGES WILL BE
# OVERWRITTEN.

/var/log/backups {
  compress
  dateext
  rotate 2
  weekly
}

Explanation

Once you start adding your own services and cronjobs to puppet you'll need a way to ensure their logs do not endlessly grow, and eventually fill, all available disk space. The common way to manage this on Unix based systems is with logrotate. While it's possible to treat these settings as static or even templated files a better, higher level, abstraction is provided by the Logrotate module.

Although Puppet itself does not provide a logrotate type we can easily install the "approved" one from the forge and use that to manage our resources.

puppet module install puppet-logrotate

...
Notice: Installing -- do not interrupt ...
/etc/puppetlabs/code/environments/production/modules
|--- puppet-logrotate (v3.2.1)
  |--- puppetlabs-stdlib (v4.25.1)
...

Now we have the type and provider we'll add the puppet code to rotate log files generated by our imaginary daily backup cron job.

class rotate {

  logrotate::rule { 'backups':
    path         => '/var/log/backups',
    rotate       => 2,
    rotate_every => 'week',
    compress     => true,
    dateext      => true,
  }

}

We will inventively call our logrotate config backups. This name is used to control the file written to disk, here puppet will create /etc/logrotate.d/backups. Each of the provided resource properties relates back to an option for the logrotate binary. You can learn more about the available options, and their behaviour, from man 8 logrotate.

Once puppet has run there should be a new logrotate configuration file on disk with the same options as you passed to the resource.

$ cat /etc/logrotate.d/backups

# THIS FILE IS AUTOMATICALLY DISTRIBUTED BY PUPPET.  ANY CHANGES WILL BE
# OVERWRITTEN.

/var/log/backups {
  compress
  dateext
  rotate 2
  weekly
}

See also