Creating your own 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 Efficiently distributing cron jobs in Chapter 5, Users and Virtual Resources, 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
.
Run the following command:
ubuntu@cookbook:~/puppet$ mkdir -p modules/custom/lib/puppet/parser/functions
Create the file
modules/custom/lib/puppet/parser/functions/random_minute.rb
with the following contents:module Puppet::Parser::Functions newfunction(:random_minute, :type => :rvalue) do |args| ...