How to generate a random number in puppet bolt

167 Views Asked by At

im trying to generate a random number in puppet bolt.

I have tried this way:


function sot::getrand(
) >> Numeric {
  $rannumber = rand(100)
  $rannumber
}

the reason why Im trying to generate a random number its because I wanted to use it in the name of a file like this:

$sot_data_file = '/tmp/sotdata${sot::getrand()}'

I got an error trying to do this way:

"Evaluation Error: Unknown function: 'rand'. (file: /home/repos/bolt_f5afm/site/sot/functions/getrand.pp, line: 3, column: 16)",

Do you know guys if there is any way to do this in an easy way? im totally new at puppet.

1

There are 1 best solutions below

0
On

Recommend implementing using a custom function. Under your bolt project, create the directory structure:

lib/puppet/functions

Create a file for the custom function lib/puppet/functions/rand.rb with the following code:

# Return a random number
Puppet::Functions.create_function(:rand) do
  # @param [Integer] max
  #     maximum random number
  # @return [Variant[Integer,Float]]
  #     random number
  # @example Calling the function
  #   rand(10)
  dispatch :generate_random_number do
    optional_param 'Integer', :max
    return_type 'Variant[Integer,Float]'
  end

  def generate_random_number(max = nil)
    rand(max)
  end
end