Saturday, December 8, 2012

Puppet Agent Cron Script

Running your puppet agent in cron is one way to avoid having to run it as a service. I don't do it on all my puppet nodes, but I do it on a few of them.

Below is my puppetagentrun.bash script that I use on linux. (Tested on CentOS and Ubuntu)
<% if kernel == 'Linux' -%>
#!/bin/bash
puppetmaster="yourpuppetserver"
# Open Source Puppet
statedir="/var/lib/puppet/state"
# Puppet Enterprise
# statedir="/var/opt/lib/pe-puppet/state"
run="puppet agent --server $puppetmaster --onetime --ignorecache --no-daemonize --no-usecacheonfailure --no-splay"
if [ -f $statedir/puppetdlock ]; then
  if test `find $statedir/puppetdlock -mmin +100`; then
    rm $statedir/puppetdlock;
  fi
fi
value=$RANDOM
while [ $value -gt 300 ] ; do
  value=$RANDOM
done
sleep $value
$run
exit
<% end -%>
Some portions you may want to change
Replace "yourpuppetserver" with your puppet masters hostname
puppetmaster="yourpuppetserver"
Change the path of your state dir as needed
# Open Source Puppet
statedir="/var/lib/puppet/state"
# Puppet Enterprise
# statedir="/var/opt/lib/pe-puppet/state"
This area checks to see if the puppet agent some how locked it self up for a long period of time. I currently have it set for 100 minutes, but feel free to change that time to anything you want.
if test `find $statedir/puppetdlock -mmin +100`; then
The last thing is a random sleep value of 5 minutes so the puppet agents don't stomp the server all at the same time. Feel free to adjust this to anything you want.
while [ $value -gt 300 ] ; do
I am using ruby templating to ensure that the kernel is linux or it doesn't get any of this code. You can remove the template variables to make it a file resource. The way it sits, you will have to save it as a "puppetmodulename/templates/puppetagentrun.bash.erb"

No comments:

Post a Comment