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


Chronological Thread 
  • From: Jesse Nelson < >
  • To:
  • Subject: [chef] Re: Create ruby variable at chef runtime at specific position in a recipe
  • Date: Sat, 3 Nov 2012 20:27:57 -0700

Create a provider for your file parsing that gets notified by the
package/file that is creating the data. Keep your parsing Class in
libraries. The provider can parse your new files data, setup your
other resources, and run them. This can also setup either a new
namespace or tuck the parsed data into the node object or in run_state
for future access. Depending on the needs you can use LWRP or native
resource/provider. I think you should be able to get away with an
LWRP+Libraries for this tho.

On Sat, Nov 3, 2012 at 7:21 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.

§