Custom Function to generate random password

756 views
Skip to first unread message

r0k5t4r

unread,
Apr 3, 2012, 2:36:06 AM4/3/12
to Puppet Users
Hi,

I'm currently trying to write a simple function to generate a random
password. I have looked at the fqdn_rand.rb script to get things going
but unfortunately my ruby skills are very bad.

I found the following code snippet that does what I need:

begin
def random_password(size = 8)
chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
(1..size).collect{|a| chars[rand(chars.size)] }.join
end

puts random_password.inspect
end

I just can't get this to work in my custom function:

cat ../lib/puppet/parser/functions/rand_pw.rb

Puppet::Parser::Functions::newfunction(:rand_pw, :type
=> :rvalue, :doc => "Returns a random string.") do |args|
chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
(1..args[0]).collect{|a| chars[rand(chars.args[0])] }.join
end

and this is the function call in the manifest:

$username2pass = rand_pw(10)

The error I get is:

err: Could not retrieve catalog from remote server: Error 400 on
SERVER: bad value for range at /etc/puppet/modules/production/users/
manifests/params.pp:11 on node

Regards,

Krzysztof Wilczynski

unread,
Apr 3, 2012, 7:36:49 AM4/3/12
to puppet...@googlegroups.com
Hi,


On Tuesday, 3 April 2012 07:36:06 UTC+1, r0k5t4r wrote:
Hi,

I'm currently trying to write a simple function to generate a random
password. I have looked at the fqdn_rand.rb script to get things going
but unfortunately my ruby skills are very bad.

r0k5t4r

unread,
Apr 3, 2012, 9:01:44 AM4/3/12
to Puppet Users
Hi,

thanks a lot. Just what I was looking for. Works like a charm. :)

Best Regards

On 3 Apr., 13:36, Krzysztof Wilczynski
<krzysztof.wilczyn...@linux.com> wrote:
> Hi,
>
> On Tuesday, 3 April 2012 07:36:06 UTC+1, r0k5t4r wrote:
>
> > Hi,
>
> > I'm currently trying to write a simple function to generate a random
> > password. I have looked at the fqdn_rand.rb script to get things going
> > but unfortunately my ruby skills are very bad.
>
> [...]
>
> Try this one:https://github.com/kwilczynski/puppet-functions/blob/master/lib/puppe...
>
> KW

Krzysztof Wilczynski

unread,
Apr 3, 2012, 9:27:33 AM4/3/12
to puppet...@googlegroups.com
Hi,

I am glad you found it useful.

KW
Reply all
Reply to author
Forward
0 new messages