This is using puppet 2.6.2 on Debian Squeeze.

I am attempting to use a custom function in a module to return random numbers 
with the hostname (translated to a number) as the random seed. These random 
numbers can be random values for hour/minute cron, to avoid the 
herd-of-elephants effect when they all run apt-get update daily.

While I'm not experienced with ruby, I've tested the function in a plain ruby 
script and it seems to work. My /tmp/t1 and /tmp/t2 files on the puppetmaster 
(see below) have the expected values in them. I have restarted puppetmaster 
after putting in the function.

How do I get the return value out of my function?

Am I missing anything else obvious?

I am reading from this guide:

http://docs.puppetlabs.com/guides/custom_functions.html

My cheap-as-heck custom function:

$ cat /etc/puppet/modules/yum_apt/lib/puppet/parser/functions/hsrand.rb
module Puppet::Parser::Functions

  newfunction(:hsrand, :type => :rvalue) do |args|

    values = Hash[
                  "a" => 1,
                  "b" => 2,
                  "c" => 3,
                  "d" => 4,
                  "e" => 5,
                  "f" => 6,
                  "g" => 7,
                  "h" => 8,
                  "i" => 9,
                  "j" => 10,
                  "k" => 11,
                  "l" => 12,
                  "m" => 13,
                  "n" => 14,
                  "o" => 15,
                  "p" => 16,
                  "q" => 17,
                  "r" => 18,
                  "s" => 19,
                  "t" => 20,
                  "u" => 21,
                  "v" => 22,
                  "w" => 23,
                  "x" => 24,
                  "y" => 25,
                  "z" => 26,
                  1 => 1,
                  2 => 2,
                  3 => 3,
                  4 => 4,
                  5 => 5,
                  6 => 6,
                  7 => 7,
                  8 => 8,
                  9 => 9,
                  "." => 1,
                  "_" => 2,
                  "-" => 3,
                 ]

    string = lookupvar('fqdn')

    File.open('/tmp/t1', 'w') {|f| f.write(string) }

    stringarray = string.split(//)

    File.open('/tmp/t2', 'w') {|f| f.write(stringarray[0]) }

    count = 0

    for i in (stringarray)
      if values[i]
        count = count + values[i]
      end
    end

    srand(count)

    rand = rand(args[0])

    return(rand)

  end

end



In the module's init.pp I attempt to use this in a cron type:

      $aptupdater = '/usr/bin/apt-get update'

      # some files and stuff in between

      cron { 'apt-get-update':
        command => $aptupdater,
        user => 'root',
        hour => hsrand(24),
        minute => hsrand(60),
        ensure => present,
      }

However, the cron job on every lab server ends up as:

# Puppet Name: apt-get-update
1 1 * * * /usr/bin/apt-get update


I greatly appreciate any assistance. I'm stumped.

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to