How to generate a random integer in a template.erb file


#1

Hi,

I am not a ruby expert but in a template I need to generate a random
integer e.g. between 0 and 10000.

How would I do that?

thanks


#2

http://www.ruby-doc.org/core-1.9.3/Random.html

Random.rand(10000)

This will cause the template to be rendered every time chef runs though (along with sending notifications) so I wouldn’t recommend doing it unless that’s the exact behavior you’re looking for.

On Wednesday, February 13, 2013 at 10:41 AM, David Montgomery wrote:

Hi,

I am not a ruby expert but in a template I need to generate a random integer e.g. between 0 and 10000.

How would I do that?

thanks


#3

Hi David,

You can use Ruby to pass that in: http://stackoverflow.com/questions/198460/how-to-get-a-random-number-in-ruby

Note however, that if you generate the random number each time you try to write the template - it will rewrite the file each run. A more stable option would be storing this as a node attribute, and re-using it each time afterwards.

Graham Christensen


Graham Christensen

On Wednesday, February 13, 2013 at 1:41 PM, David Montgomery wrote:

Hi,

I am not a ruby expert but in a template I need to generate a random integer e.g. between 0 and 10000.

How would I do that?

thanks


#4

Can I generate the number ad a variable and then create a lock file? e.g.
not_if {File.exists?("#{Chef::Config[:file_cache_path]}/random.lock")}

If so what would the block in the recipe look like? Then I can pass the
variable to the template.

Thanks

On Thu, Feb 14, 2013 at 2:44 AM, Daniel Condomitti daniel@condomitti.comwrote:

http://www.ruby-doc.org/core-1.9.3/Random.html

Random.rand(10000)

This will cause the template to be rendered every time chef runs though
(along with sending notifications) so I wouldn’t recommend doing it unless
that’s the exact behavior you’re looking for.

On Wednesday, February 13, 2013 at 10:41 AM, David Montgomery wrote:

Hi,

I am not a ruby expert but in a template I need to generate a random
integer e.g. between 0 and 10000.

How would I do that?

thanks


#5

I urge you to consider the idea of using a node attribute, which solves this problem simply, and without extra filesystem dependencies. This snippet here will set the mysql server’s ID once, and then reuse it from there: https://github.com/nationalfield/chef-mysql-ext/blob/master/recipes/default.rb#L1


Graham Christensen

On Wednesday, February 13, 2013 at 1:48 PM, David Montgomery wrote:

Can I generate the number ad a variable and then create a lock file? e.g. not_if {File.exists?("#{Chef::Config[:file_cache_path]}/random.lock")}

If so what would the block in the recipe look like? Then I can pass the variable to the template.

Thanks

On Thu, Feb 14, 2013 at 2:44 AM, Daniel Condomitti <daniel@condomitti.com (mailto:daniel@condomitti.com)> wrote:

http://www.ruby-doc.org/core-1.9.3/Random.html

Random.rand(10000)

This will cause the template to be rendered every time chef runs though (along with sending notifications) so I wouldn’t recommend doing it unless that’s the exact behavior you’re looking for.

On Wednesday, February 13, 2013 at 10:41 AM, David Montgomery wrote:

Hi,

I am not a ruby expert but in a template I need to generate a random integer e.g. between 0 and 10000.

How would I do that?

thanks


#6

Oh I see…thanks all!

On Thu, Feb 14, 2013 at 2:52 AM, Graham Christensen graham@grahamc.comwrote:

I urge you to consider the idea of using a node attribute, which solves
this problem simply, and without extra filesystem dependencies. This
snippet here will set the mysql server’s ID once, and then reuse it from
there:
https://github.com/nationalfield/chef-mysql-ext/blob/master/recipes/default.rb#L1


Graham Christensen

On Wednesday, February 13, 2013 at 1:48 PM, David Montgomery wrote:

Can I generate the number ad a variable and then create a lock file? e.g.
not_if {File.exists?("#{Chef::Config[:file_cache_path]}/random.lock")}

If so what would the block in the recipe look like? Then I can pass the
variable to the template.

Thanks

On Thu, Feb 14, 2013 at 2:44 AM, Daniel Condomitti daniel@condomitti.comwrote:

http://www.ruby-doc.org/core-1.9.3/Random.html

Random.rand(10000)

This will cause the template to be rendered every time chef runs though
(along with sending notifications) so I wouldn’t recommend doing it unless
that’s the exact behavior you’re looking for.

On Wednesday, February 13, 2013 at 10:41 AM, David Montgomery wrote:

Hi,

I am not a ruby expert but in a template I need to generate a random
integer e.g. between 0 and 10000.

How would I do that?

thanks