0
votes

I try to read out the current recipe name while chef-client run and to store it in a variable or node attribute wihtin an recipe. Until yet i just found a way storing it into a node attribute but it always fails. This is my code:

  ruby_block "Fetch Recipe Name From Run List" do
    block do
      Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
      s = shell_out("echo \"#{node['expanded_run_list']}\" | awk -F '::' '{print substr($3, 1, length($3)-1)}'" )
      node.default['sftp-selfmade']['extracted_recipe'] = s.stdout
    end
  end

  extracted_recipe = node['sftp-selfmade']['extracted_recipe']

  # To debug the output of the node attribute.
  execute 'TEST' do
    command "echo \"TEST #{extracted_recipe}\""
  end

Output:

  * execute[TEST] action run
    [execute] TEST 
    - execute echo "TEST "

Output should be:

- execute echo "TEST <Name-Of-Extracted-Recipe>"

I tried lot's of things as also storing the s.stdout output in a variable but this throws an NoMethodError during compiling stage. Also tried to use stronger values like node.override - this works but only by setting node.normal first and the setting it to node.override but this is not a satisfying solution to do this everytime within cookbook code again for deploying to new hosts. Tried also a solution reloading OHAI. But this also did not work. On a completely new host it also doesn't work after a 2nd chef-client run in case of attributes have been set then after first run.

Is there somebody who can help me out?

1

1 Answers

0
votes

Found out the following solution:

expandedrecipe = node['expanded_run_list'].select{ |e| e.include? 'sftp-selfmade' }.first.split('::').last

That does the trick.