Creating custom functions
If you've read the recipe Using GnuPG to encrypt secrets in Chapter 4, Working with Files and Packages, then you've already seen an example of a custom function (in that example, we created a secret
function, which shelled out to GnuPG). Let's look at custom
functions in a little more detail now and build an example.
How to do it...
If you've read the recipe Distributing cron jobs efficiently in Chapter 6, Managing Resources and Files, you might remember that we used the inline_template
function to set a random time for cron jobs to run, based on the hostname of the node. In this example, we'll take that idea and turn it into a custom function called random_minute
:
- Create the file
modules/cookbook/lib/puppet/parser/functions/random_minute.rb
with the following contents:module Puppet::Parser::Functions newfunction(:random_minute, :type => :rvalue) do |args| lookupvar('hostname').sum % 60 end end
- Modify your
site.pp
file...