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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
hello, functions in puppet are always executed on the puppet master. So in your case, every node gets the same value since you''re always basing your calculation on the puppet master''s fqdn. You might want to look into transforming your function (master-side) into a fact (client-side) or, you could also use the builtin function fqdn_rand, which gives you a random number with the machine''s fqdn as a seed for the pseudo-random number generator. It might save you some work: http://docs.puppetlabs.com/references/2.6.8/function.html#fqdnrand On 11-09-16 02:21 PM, Christopher Wood wrote:> 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. >-- Gabriel Filion -- 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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Fri, Sep 16, 2011 at 09:04:31PM -0400, Gabriel Filion wrote:> hello, > > functions in puppet are always executed on the puppet master. So in your > case, every node gets the same value since you''re always basing your > calculation on the puppet master''s fqdn.I certainly appreciate the tip, but I''m fairly sure that the fqdn used is the client''s. Every time I kick a client host /tmp/t1 on the puppetmaster changes to the fqdn of that host. As well using my puppetmaster''s fqdn as the seed in a separate ruby script gives me a series of random values that doesn''t start with 1,1. The "Using Facts and Variables" section of http://docs.puppetlabs.com/guides/custom_functions.html shows how to use lookupvar() to obtain client facts.> You might want to look into transforming your function (master-side) > into a fact (client-side) > > > or, you could also use the builtin function fqdn_rand, which gives you a > random number with the machine''s fqdn as a seed for the pseudo-random > number generator. It might save you some work: > > http://docs.puppetlabs.com/references/2.6.8/function.html#fqdnrandI might if I wanted simple random values, but I''m hoping to branch out eventually. Things like "random + 60" or "seed on the fqdn if it contains a certain string, otherwise seed on the hostname only".> On 11-09-16 02:21 PM, Christopher Wood wrote: > > 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. > > > > -- > Gabriel Filion >-- 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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On 11-09-16 09:59 PM, Christopher Wood wrote:> Every time I kick a client host /tmp/t1 on the puppetmaster changes to the fqdn of that host. As well using my puppetmaster''s fqdn as the seed in a separate ruby script gives me a series of random values that doesn''t start with 1,1. The "Using Facts and Variables" section of http://docs.puppetlabs.com/guides/custom_functions.html shows how to use lookupvar() to obtain client facts.oh, I didn''t know about the purpose of lookupvar().. I guess I''ll sleep a little less stupid tonight :) sorry for the noise then. -- Gabriel Filion -- 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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.