Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include_recipe inside a ruby_block of a chef recipe

I have a recipe that sets a variable inside a ruby_block and needs to use that variable as an input attribute for a recipe. How can I use the include_recipe after the ruby_block has been executed?

Thanks

ruby_block "evaluate_config" do #~FC014
 block do

  file = File.read('/opt/config/baselogging.json')
  data = JSON.parse(file)

  node.default['kibana']['apache']['basic_auth_username'] = data['KibanaUser']
  node.default['kibana']['apache']['basic_auth_password'] = data['KibanaPassword']

  include_recipe 'kibana'

 end
end
like image 772
JoseOlcese Avatar asked Mar 03 '15 19:03

JoseOlcese


2 Answers

To include a recipe from a ruby_block, you must call it using run_context.

For example:

ruby_block "evaluate_config" do #~FC014
 block do
   ...
   #include_recipe 'kibana'
   run_context.include_recipe "cookbook::recipe"
 end
end
like image 76
manoelhc Avatar answered Oct 02 '22 13:10

manoelhc


You can read and set attributes from ruby block, and than after it you can include recipe like:

ruby_block "evaluate_config" do #~FC014
 block do   
  file = File.read('/opt/config/baselogging.json')
  data = JSON.parse(file)

  node.set['kibana']['apache']['basic_auth_username'] = data['KibanaUser']
  node.set['kibana']['apache']['basic_auth_password'] = data['KibanaPassword']   
 end
end

include_recipe 'kibana'
like image 40
rastasheep Avatar answered Oct 02 '22 12:10

rastasheep