random hex string in erb template

273 views
Skip to first unread message

tobias...@gmail.com

unread,
Nov 25, 2015, 9:48:00 AM11/25/15
to Puppet Users
I want to create a random hex string in the format

041f798-a5f

inclusive the dash in a XML file.

I have tried using a self written shell script which generates the string with

<hid><%= exec('/var/somedir/somescript') %></hid>

in the template. That unfortunately didn't work.

I suspect I have to write a small embedded ruby template / code snipplet to generate this structure but after some web search I still have no idea how to do that in Embedded Ruby. Does somebody have a short erb suggestion?




Peter Kristolaitis

unread,
Nov 25, 2015, 10:36:24 AM11/25/15
to puppet...@googlegroups.com
<% range = [*'0'..'9',*'A'..'Z',*'a'..'z'] -%>
<hid><%= Array.new(7){range.sample}.join + "-" + Array.new(3){range.sample}.join %></hid>

The "<% range ... -%>" can be anywhere in your template as long as it's before where you generate the random value.  I usually put stuff like that near the top of the template.  If you're generating multiple random values you only need to specify it once.
--
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/17c87311-bf8c-4aba-98ed-af30f62e3aca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Kristolaitis

unread,
Nov 25, 2015, 10:39:02 AM11/25/15
to puppet...@googlegroups.com
Just realized my code is slightly wrong for you since you wanted hex values rather than alphanumeric string.

The first line should be:

<% range = [*'0'..'9',*'a'..'f'] -%>

tobias...@gmail.com

unread,
Nov 25, 2015, 11:18:08 AM11/25/15
to Puppet Users
Works great. Thanks.
Reply all
Reply to author
Forward
0 new messages