[chef] Re: Re: Re: Re: How do I modify a local variable through ruby_block?


Chronological Thread 
  • From: Torben Knerr < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: How do I modify a local variable through ruby_block?
  • Date: Sun, 24 Jun 2012 20:13:42 +0200

Hi Daniel,

you have to understand the difference between the compile phase (resources are created and put into the resource collection) and the execution phase (all resources in the resource collection are "executed"), it is explained in more detail here:
http://wiki.opscode.com/display/chef/Anatomy+of+a+Chef+Run 

If you really need to evaluate/run the resource during compile time, have a look here:
http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time

HTH,
Torben


On Sat, Jun 23, 2012 at 10:08 AM, Daniel S. Kim < " target="_blank"> > wrote:
Well, in simple terms, I have two tasks. First one is to mount a virtual disk onto my node, which is done through execute. Afterward, I want to run ruby code to iterate a mounted directory and run a regular _expression_ or search by file name. However, I cannot seem to get the result of this regular _expression_ matching nor the exact file name match. If I use Chef::Log.info() to print out the file name within the ruby block, it shows the result just fine. However, I cannot get this value outside this block. Let me know if any of you got a solution for this. I am more used to C language series and Java, so Ruby and Chef, both, are somewhat unfamiliar to me yet. Help will be much appreciated since I am hoping to solve this before tonight.


On Fri, Jun 22, 2012 at 9:04 PM, AJ Christensen < " target="_blank"> > wrote:
Some people use the node.run_state hash to store stuff like this in.

Why must you modify the scope-local variable in the ruby block?

--AJ

On 23 June 2012 15:56, Adam Jacob < " target="_blank"> > wrote:
> The issue here is that Chef builds a collection of resources, which it
> then iterates over. The block in the ruby block is not evaluated until
> much later. What exactly are you trying to accomplish?
>
> Adam
>
> On Fri, Jun 22, 2012 at 3:04 PM, Daniel S. Kim < " target="_blank"> > wrote:
>> Hello all,
>>
>> I am having hard time with a variable modification. See the snippet below:
>>
>> subdir = ""
>>
>> ruby_block "read target archive directory name" do
>>   block do
>>     Dir.foreach("/snapshot/#{dbSnapshotId}") do |f|
>>       unless(f.to_s.eql? ".")
>>         unless (f.to_s.eql? "..")
>>           Chef::Log.info("Timestamp: " + f.to_s)
>>           subdir = f.to_s
>>           node[:RestoreSubdir] = subdir
>>           node.save
>>         end
>>       end
>>     end
>>   end
>>   action :create
>> end
>>
>>
>> After the ruby_block, subdir is still an empty string. However, Chef::Log
>> shows a value otherwise. I also tried these other method in order to get
>> this String outside the ruby_block scope:
>> 1. script resource => variable modification undone once out of scope
>> 2. ruby_block resource => variable modification undone once out of scope
>> 3. separate recipe with ruby code => ruby code still runs 1st before
>> mounting the drive
>> 4. separate recipe with ruby_block/script => still cannot see the variable
>> modification once out of scope
>> 5. node.save within ruby_block => save doesn't occur until chef-client is
>> done running all the resources
>> 6. databag.save within ruby_block => save doesn't occur until chef-client is
>> done running all the resources
>>
>>
>> Any suggestion?
>>
>>
>>
>>
>> Regard,
>>
>> --
>> Daniel S. Kim
>>
>
>
>
> --
> Opscode, Inc.
> Adam Jacob, Chief Customer Officer
> T: (206) 619-7151 E: " target="_blank">



--
Daniel S. Kim





Archive powered by MHonArc 2.6.16.

§