Articles‎ > ‎Puppet‎ > ‎

Puppet autoclassifier

If you have environment or network information somehow encoded into hostname you might have a chance to have zero maintenance puppet server config. All you need is to write autoclassifier script and plug it in into puppet server config. Lets create script which will look something like this:
#!/bin/sh
#
h=`echo $1 | cut -f1 -d.`
if [ "z${h}" == "zdefault" ]; then
      echo '---'
      echo 'classes:'
      echo " - default"
      exit 0
elif [ `echo ${h} | grep -c '^sea'` -eq 1 ] ; then
   office='seattle'
elif [ `echo ${h} | grep -c '^sfa'` -eq 1 ] ; then
   office='sanfrancisco'
else
      exit 1
fi
#   echo "  - ${h}"
echo '---'
echo 'classes:'
echo "  - ${office}-office"
exit 0

Test script by supplying host name to it as parameter, check that script has valid YAML output. Save script somewhere in /etc/puppet/scripts/, define this script in /etc/puppet/puppet.conf in [puppetmasterd] section as external_nodes=/etc/puppet/scripts/classifier.sh. Now all you need to do is to define all you classes in site.pp (don't forget to define default class) then enjoy servers being automatically assigned to classes based on regexp against their hostname.
Comments