My custom puppet function changes values

30 views
Skip to first unread message

p

unread,
May 18, 2016, 9:10:45 AM5/18/16
to Puppet Users
Hi Everyone,

My custom puppet code changes values randomly. Why?

This code creates a random port for me based on the argument's hash. The idea is that this will create a TCP port for me based on the name of the argument I give it (for instance the FQDN) so that it is unique, above 1024 but below 65535. I don't want to have to worry about port numbers, it should just calculate the same every time.

module Puppet::Parser::Functions
  newfunction(:ip_port, :type => :rvalue) do |args|
    args[0].hash % 64511 + 1024
  end
end


I use it in a manifest this way:
$ip_port = ip_port('www.example.com')

The puppet agent runs every 30 minutes by default. It's a master/slave v3.8 setup. When I run it manually, the value always comes back the same. But when I leave it alone, at some point the value changes. I use this to create ports for haproxy. Thanks for your help.


Peter

Trevor Vaughan

unread,
May 18, 2016, 10:35:55 AM5/18/16
to puppet...@googlegroups.com
Hi Peter,

Object.hash in Ruby provides the hash of the Object ID, not a consistent cryptographic hash of the string provided.

Try it in two different IRB sessions and you'll see what I mean. It will change between garbage collection runs and/or at every invocation of Puppet if done in cron.

Trevor



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/d2551534-236d-4598-b288-1faa7d3c6bb0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Trevor Vaughan
Vice President, Onyx Point, Inc

-- This account not approved for unencrypted proprietary information --

p

unread,
May 18, 2016, 12:58:15 PM5/18/16
to Puppet Users
Thanks, Trevor. I did and as you had predicted they are different.

p

unread,
May 18, 2016, 1:16:19 PM5/18/16
to Puppet Users
Any suggestions on how I can get the hash of the string content itself so I can get this function to work? I really just don't want to manage ports anymore.


On Wednesday, May 18, 2016 at 7:35:55 AM UTC-7, Trevor Vaughan wrote:

p

unread,
May 18, 2016, 7:37:34 PM5/18/16
to Puppet Users
I'm doing this instead:
module Puppet::Parser::Functions
  newfunction(:ip_port, :type => :rvalue) do |args|
    require 'zlib'
    Zlib.crc32(args[0]) % 64511 + 1024
  end
end

Trevor Vaughan

unread,
May 18, 2016, 9:35:49 PM5/18/16
to puppet...@googlegroups.com
This should work in all versions of Ruby (YMMV)

'foo.bar.baz'.each_byte.map{|x| x}.join.to_i % 64511 + 1024

Trevor


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages