random number in a file

1,127 views
Skip to first unread message

Allan Marcus

unread,
Aug 11, 2009, 3:07:40 PM8/11/09
to puppet...@googlegroups.com
Hi,

I would like to have puppet manage a file on each client. The file is
the same for all clients, but has two random numbers in it.

I assume I can use something like fqdn_rand(0-59) to generate a random
number between 0 and 59, right?

My main question is how to I get puppet to not think the file is
different every time puppetd runs? Would I use a template or a file
def and contents?

---
Thanks,

Allan Marcus
505-667-5666

Peter Meier

unread,
Aug 11, 2009, 6:03:56 PM8/11/09
to puppet...@googlegroups.com
Hi

neither will help. either you make your random function depending on a
seed, which means it isn't really random anymore. Or if you create the
file, replace => false might be your friend. But then for sure if you
change your other content it won't adjust them as well.

cheers pete

Greg

unread,
Aug 11, 2009, 10:05:51 PM8/11/09
to Puppet Users
Depending on how you are doing it, you could use:

exec "randno" {
creates => "/tmp/foo",
}

Let me guess, you are trying to get cron to run at a random but
consistent time, yes?
From what I've read (haven't tried it myself yet) the best way is to
map the number from
a consistent thing about the host... Maybe get the last octet from the
IP address of the
node and do an IP % 60 perhaps... Its not random, but it does spread
out a cron job over
the hour...

Since its on my todo list as well, I haven't got an example of exactly
how to do this
to show you unfortunately...

Greg

Larry Ludwig

unread,
Aug 11, 2009, 11:02:22 PM8/11/09
to puppet...@googlegroups.com
Here is one method to perform puppet runs via cron:

http://reductivelabs.com/trac/puppet/wiki/Recipes/cron

It also allows you to manually tweak the times each node runs.

-L

--
Larry Ludwig
Reductive Labs

Allan Marcus

unread,
Aug 12, 2009, 10:47:49 AM8/12/09
to puppet...@googlegroups.com
Thanks to all of you for your help. This cron example is close to
where I was heading. Macs can use cron, but it's not encouraged, so I
will convert the concept to launchd.

---
Thanks,

Allan Marcus
505-667-5666



Brice Figureau

unread,
Aug 12, 2009, 11:23:33 AM8/12/09
to puppet...@googlegroups.com
On Tue, 2009-08-11 at 13:07 -0600, Allan Marcus wrote:
> Hi,
>
> I would like to have puppet manage a file on each client. The file is
> the same for all clients, but has two random numbers in it.
>
> I assume I can use something like fqdn_rand(0-59) to generate a random
> number between 0 and 59, right?

Right. But with a caveat: the random number is not really random. In
fact the random generator is seeded with the node fqdn. Since this is a
constant, the generated random number won't change from run to run for a
given node.

> My main question is how to I get puppet to not think the file is
> different every time puppetd runs? Would I use a template or a file
> def and contents?


file {
"/path/to/random": content => fqdn_rand(60)
}

Will produce a file containing a random number that will never change
for a given node.

Or if you want to manage cron resource directly:
cron {
"mycron":
command => "/usr/local/bin/backup-logical",
hour => '*',
minute => fqdn_rand(60),
}

Hope that helps,
--
Brice Figureau
My Blog: http://www.masterzen.fr/

michael....@hycom.org

unread,
Jul 21, 2015, 3:31:46 PM7/21/15
to puppet...@googlegroups.com

Right. But with a caveat: the random number is not really random. In
fact the random generator is seeded with the node fqdn. Since this is a
constant, the generated random number won't change from run to run for a
given node.

 

Or if you want to manage cron resource directly:

cron {
 "mycron":
        command => "/usr/local/bin/backup-logical",
        hour => '*',
        minute => fqdn_rand(60),
}



Another helpful tip is to add an additional SEED to 'fqdn_rand' so that each subsequent cron job managed with puppet (each with a unique name) is stagger within the hour.

Usage: fqdn_rand(MAX, [SEED]). MAX is required and must be a positive integer; SEED is optional and may be any number or string.

  cron { "manual-puppet":
    command => "/usr/bin/puppet apply --modulepath=/Net/puppet/modules /Net/puppet/manifests/site.pp",
    user => root,
    hour => '*',
    minute => fqdn_rand(60, $name),
    ensure => present,
  }

Without the "$name" SEED, each cron job using 'fqdn_rand(60)' to randomize the minute would run at the exact same time on a given server. Now our multiple cron jobs are randomly distributed with the added SEED value.

Reply all
Reply to author
Forward
0 new messages