[chef] Re: [CHEF]Create random file and assign to a variable in recipe


Chronological Thread 
  • From: Sean OMeara < >
  • To: " " < >
  • Subject: [chef] Re: [CHEF]Create random file and assign to a variable in recipe
  • Date: Thu, 23 Jul 2015 09:38:10 -0400

This one is tricky...

You'll need to calculate your string(s) during the compilation phase or your recipe, and pass then use them in your resources. Something like this.

my_string = 'ABC123etcetc'

directory "/stuff/#{my_string}" do
  action :create
end

file "/stuff/#{my_string}/file_1" do
  action :create
end

file "/stuff/#{my_string}/file_2" do
  action :create
end

However, you need need to be careful how you generate my_string.
You'll be tempted to make them actually random

require 'securerandom'
my_string = SecureRandom.hex

This won't work, because my_string will change on subsequent chef-client runs. 

You'll need to use something like:
 
require 'digest'

seed = "constant"
my_string = Digest::SHA1.hexdigest(seed)[0,8]

Hope that helps.


-s

On Thu, Jul 23, 2015 at 5:27 AM, Jacobus van Heerden < " target="_blank"> > wrote:
Good Day

In my recipe I want to create a filename with a random name in say either a ruby block or a bash block. I then want to assign that created filename to a variable and use that variable in the rest of my recipe.

How do I go about doing that?

Regards





Archive powered by MHonArc 2.6.16.

§