Chef Recipe Help

Hello,

I’m learning Chef and need help in below mentioned scenario.

Here is the Chef recipe that I’m using.

in attributes/default.rb file
node[‘my_recipe’][‘sample’] = {‘tool1’ => [‘toolname’, ‘tool1.exe’,
’/sc Weekly /d SUN /st 1:00]}

#global variable
$schedule_name=’’
$executable =’’
$schedule = ‘’

node[‘my_recipe’][‘sample’].each do |tool, infoList|
ruby_block “assign_variable” do
block do
$schedule_name = infoList.at(0)
$executable = infoList.at(1)
$schedule = infoList.at(2)

		puts "schedule_name = #{$schedule_name}"
		puts "executable = #{$executable}"
		puts "schedule = #{$schedule}"
	end
	action :create
	notifies :run, "execute[schedule_#{tool}]"
end

ruby_block "display_variable" do
	block do
		puts "schedule_name = #{$schedule_name}"
		puts "executable = #{$executable}"
		puts "schedule = #{$schedule}"
	end
end

execute "schedule_#{tool}" do
	command "schtasks /create /tn \"#{$schedule_name}\" /tr

“#{node[‘my_recipe’][‘tools_dir’]}\#{tool}\#{$executable}”
#{$schedule}"
action :nothing
end
end

In “ruby_block “assign_variable” do”, I’m asigning values to global variable.

In “ruby_block “display_variable” do”, I’m able to fetch the values of
these variables successfully.

However, in “execute “schedule_#{tool}” do”, when I’m using these
global variable it returns me empty.

Could you please assist me resolving me?

Regards,
Ravindra Chandrakar

Ravindra ravindra.chandrakar@gmail.com writes:

Hello,

I'm learning Chef and need help in below mentioned scenario.

Here is the Chef recipe that I'm using.

in attributes/default.rb file
node['my_recipe']['sample'] = {'tool1' => ['toolname', 'tool1.exe',
'/sc Weekly /d SUN /st 1:00]}

#global variable
$schedule_name=''
$executable =''
$schedule = ''

node['my_recipe']['sample'].each do |tool, infoList|
ruby_block "assign_variable" do
block do
$schedule_name = infoList.at(0)
$executable = infoList.at(1)
$schedule = infoList.at(2)

  	puts "schedule_name = #{$schedule_name}"
  	puts "executable = #{$executable}"
  	puts "schedule = #{$schedule}"
  end
  action :create
  notifies :run, "execute[schedule_#{tool}]"

end

ruby_block "display_variable" do
block do
puts "schedule_name = #{$schedule_name}"
puts "executable = #{$executable}"
puts "schedule = #{$schedule}"
end
end

execute "schedule_#{tool}" do
command "schtasks /create /tn "#{$schedule_name}" /tr
"#{node['my_recipe']['tools_dir']}\#{tool}\#{$executable}"
#{$schedule}"
action :nothing
end
end

In "ruby_block "assign_variable" do", I'm asigning values to global variable.

In "ruby_block "display_variable" do", I'm able to fetch the values of
these variables successfully.

However, in "execute "schedule_#{tool}" do", when I'm using these
global variable it returns me empty.

Could you please assist me resolving me?

From

The ruby_block resource is used to execute Ruby code during a
chef-client run. Ruby code in the ruby_block resource is evaluated with
other resources during convergence, whereas Ruby code outside of a
ruby_block resource is evaluated before other resources, as the recipe
is compiled.

Regards,
Noorul