How do I modify a local variable through ruby_block?


#1

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 :createend

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


#2

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 ksw2599@gmail.com 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: adam@opscode.com


#3

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 adam@opscode.com 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 ksw2599@gmail.com 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: adam@opscode.com


#4

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 aj@junglist.gen.nz 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 adam@opscode.com 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 ksw2599@gmail.com
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: adam@opscode.com


Daniel S. Kim


#5

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 ksw2599@gmail.com 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 aj@junglist.gen.nzwrote:

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 adam@opscode.com 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 ksw2599@gmail.com
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
  1. separate recipe with ruby code => ruby code still runs 1st before
    mounting the drive
  2. 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: adam@opscode.com


Daniel S. Kim


#6

Thank you very much. Since I cannot seem to modify local variables inside
the ruby_block, I think I will have to evaluate and run resources during
compile time.

On Sun, Jun 24, 2012 at 1:13 PM, Torben Knerr ukio@gmx.de wrote:

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 ksw2599@gmail.com 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 aj@junglist.gen.nzwrote:

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 adam@opscode.com 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 ksw2599@gmail.com
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
  1. separate recipe with ruby code => ruby code still runs 1st before
    mounting the drive
  2. 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: adam@opscode.com


Daniel S. Kim


Daniel S. Kim