ruby block not executing code, only printing output

572 Views Asked by At

I have two chef recipes I want to run in a defined order. First is the install recipe, then it's the config.

Here is the code invoking the recipes:

  ruby_block "bowbridge_config" do
    block do
      run_context.include_recipe "ids::bowbridge_config"
    end
    action :nothing
  end

  ruby_block "bowbridge_install" do
    block do
      run_context.include_recipe "sap-bowbridge::default"
    end
    notifies :run, 'ruby_block[bowbridge_config]', :delayed
  end

I successfully execute the install recipe before the config recipe but when the config recipe is executing only the prints are being printed.

config recipe code:

  mcaf_lib = find_file "/opt/bowbridge/libAVB*_mcaf.so"
  Chef::Log.info("==> bowbridge_config mcaf_lib is #{mcaf_lib}. Vsi file is #{vsi_file}")
  bb_cfg = File.basename(find_file "/opt/bowbridge/bbvsa*.cfg")
  Chef::Log.info("==> bowbridge_config recipe is triggered")

# Setup bowbridge config file
  directory "/etc/bowbridge" do
  end
  file "/etc/bowbridge/" + bb_cfg do
    owner 'root'
    group 'root'
    mode 0755
    content ::File.open("/opt/bowbridge/" + bb_cfg).read
    action :create
  end
  Chef::Log.info("==> bowbridge_config before link creation")
  link "/lib64/libvsa.so" do
    to "#{mcaf_lib}"
  end

The above code shows this output:

[2017-02-24T11:25:36+00:00] INFO: ruby_block[bowbridge_install] sending run action to ruby_block[bowbridge_config] (delayed) 
[2017-02-24T11:25:36+00:00] INFO: Processing ruby_block[bowbridge_config] action run (ids::default line 82) 
[2017-02-24T11:25:37+00:00] INFO: ==> bowbridge_config recipe is triggered 
[2017-02-24T11:25:37+00:00] INFO: ==> bowbridge_config before link creation 
[2017-02-24T11:25:37+00:00] INFO: ruby_block[bowbridge_config] called

No /etc/bowbridge directory is being created and no link inside /lib64 is created. What could I be doing wrong?

2

There are 2 best solutions below

4
On BEST ANSWER

I would not recommend using ruby blocks this way, just add the recipes in the order you want Chef to execute them. In this case:

include_recipe 'sap-bowbridge::default'
include_recipe 'ids::bowbridge_config'

Now, if you need some values to be obtained during execution of the Chef-client, you can use a lazy evaluation. There is plenty of information in Chef website.

As an example, check this:

link '/lib64/libvsa.so' do
    to lazy { find_file '/opt/bowbridge/libAVB*_mcaf.so' }
end

Finally, try to use simple quotes ' ' rather than double " " if there is no need for interpolation or adding variables to the text, also related to this, please use:

"/etc/bowbridge/#{bb_cfg}"

Rather than

"/etc/bowbridge/" + bb_cfg
7
On

Move from your actual two ruby block to this:

include_recipe "sap-bowbridge::default"
include_recipe "ids::bowbridge_config"

You'll have the same effect, chef honor the runlist order, and the inclusion order so there's no need for the over complexity of your two ruby_block.

You're actual code will run at converge time, the config recipe running late in the run due to the notification delayed. If that's your need, write a custom_resource instead of tweaking around the runlist.