Puppet Function: extlib::random_password

Defined in:
lib/puppet/functions/extlib/random_password.rb
Function type:
Ruby 4.x API

Overview

extlib::random_password(Integer[1] $length)String

A function to return a string of arbitrary length that contains randomly selected characters.

Examples:

Calling the function

random_password(42)

Parameters:

  • length (Integer[1])

    The length of random password you want generated.

Returns:

  • (String)

    The random string returned consists of alphanumeric characters excluding ‘look-alike’ characters.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet/functions/extlib/random_password.rb', line 40

Puppet::Functions.create_function(:'extlib::random_password') do
  # @param length The length of random password you want generated.
  # @return [String] The random string returned consists of alphanumeric characters excluding 'look-alike' characters.
  # @example Calling the function
  #   random_password(42)
  dispatch :random_password do
    param 'Integer[1]', :length
    return_type 'String'
  end

  def random_password(length)
    # These are quite often confusing ...
    ambiguous_characters = %w[0 1 O I l]

    # Get allowed characters set ...
    set = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
    set -= ambiguous_characters

    # Shuffle characters in the set at random and return desired number of them ...
    Array.new(length) do |_i|
      set[rand(set.length)]
    end.join
  end
end