[chef] Re: Create ruby variable at chef runtime at specific position in a recipe


Chronological Thread 
  • From: KC Braunschweig < >
  • To:
  • Subject: [chef] Re: Create ruby variable at chef runtime at specific position in a recipe
  • Date: Sat, 3 Nov 2012 10:21:48 -0700

Yeah that won't work. You could write the data to the node object which would be accessible from the later resource but then you have crap in your node object you probably don't need. Instead you could write an LWRP that reads the data and creates the file resource.

KC

On Nov 3, 2012 7:22 AM, < "> > wrote:
I have a recipe in which one resource creates a file. After that I want to use my self written object from libraries to parse this file and extract some values. This values then I want to use in the coming resources. But when I use a ruby_block at the specific position in the recipe to create my object that holds simply the values as a hash, the hash is empty in the next resource. Here is a short example to explain.

package "some_special_package" # Produce the file "key_value_fil" in "/path/to/the/file/i/want/to/parse"

ruby_block "Create my object" do
  block do
    my_hash_obj_with_the_key_value_pairs = (MyObj.new("/path/to/the/file/i/want/to/parse/key_value_file")).parse_file # returns a hash like: {:mypath => "/etc/some_other_dir"}
  end
end

file "touch some other file" do
  path "#{my_hash_obj_with_the_key_value_pairs[:mypath]}/file_to_touch"
  action :touch
end

I know what is the reason for that. Chef runs in two phases and in the first phase the object "MyObj" is created, but at this point the file does not exist. So the generated hash is empty. In the second phase the access to "my_hash_obj_with_the_key_value_pairs[:mypath]" returns nil and so on. In my last try I used the "node.run_state" variable, but there is the same problem.

So someone how can help me?

Oleg



Archive powered by MHonArc 2.6.16.

§