2
2
# fqdn_rotate.rb
3
3
#
4
4
5
- module Puppet ::Parser ::Functions
6
- newfunction ( :fqdn_rotate , :type => :rvalue , :doc => <<-EOS
7
- Rotates an array a random number of times based on a nodes fqdn.
8
- EOS
9
- ) do |arguments |
5
+ Puppet ::Parser ::Functions . newfunction (
6
+ :fqdn_rotate ,
7
+ :type => :rvalue ,
8
+ :doc => "Usage: `fqdn_rotate(VALUE, [SEED])`. VALUE is required and
9
+ must be an array or a string. SEED is optional and may be any number
10
+ or string.
11
+
12
+ Rotates VALUE a random number of times, combining the `$fqdn` fact and
13
+ the value of SEED for repeatable randomness. (That is, each node will
14
+ get a different random rotation from this function, but a given node's
15
+ result will be the same every time unless its hostname changes.) Adding
16
+ a SEED can be useful if you need more than one unrelated rotation." ) do |args |
10
17
11
18
raise ( Puppet ::ParseError , "fqdn_rotate(): Wrong number of arguments " +
12
- "given (#{ arguments . size } for 1)" ) if arguments . size < 1
19
+ "given (#{ args . size } for 1)" ) if args . size < 1
13
20
14
- value = arguments . shift
21
+ value = args . shift
15
22
require 'digest/md5'
16
23
17
24
unless value . is_a? ( Array ) || value . is_a? ( String )
@@ -31,7 +38,7 @@ module Puppet::Parser::Functions
31
38
32
39
elements = result . size
33
40
34
- seed = Digest ::MD5 . hexdigest ( [ lookupvar ( '::fqdn' ) , arguments ] . join ( ':' ) ) . hex
41
+ seed = Digest ::MD5 . hexdigest ( [ lookupvar ( '::fqdn' ) , args ] . join ( ':' ) ) . hex
35
42
# deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
36
43
if Puppet ::Util . respond_to? ( :deterministic_rand )
37
44
offset = Puppet ::Util . deterministic_rand ( seed , elements ) . to_i
@@ -51,7 +58,6 @@ module Puppet::Parser::Functions
51
58
result = string ? result . join : result
52
59
53
60
return result
54
- end
55
61
end
56
62
57
63
# vim: set ts=2 sw=2 et :
0 commit comments