ruby块没有执行代码,只打印输出

时间:2017-02-24 12:20:32

标签: ruby chef chef-recipe ruby-block

我想要以定义的顺序运行两个厨师食谱。首先是安装配方,然后是配置。

以下是调用食谱的代码:

  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

我在配方配方之前成功执行了安装配方,但是当执行配方配方时,只打印了打印件。

配置食谱代码:

  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

上面的代码显示了这个输出:

[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

没有创建/ etc / bowbridge目录,并且没有创建/ lib64内的链接。我能做错什么?

2 个答案:

答案 0 :(得分:1)

从实际的两个红宝石块移动到此:

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

您将获得相同的效果,厨师尊重运行列表顺序和包含顺序,因此不需要两个ruby_block的过度复杂。

您的实际代码将在收敛时运行,由于通知延迟,配置配方在运行后期运行。如果这是您的需要,请写一个custom_resource而不是在运行列表周围进行调整。

答案 1 :(得分:1)

我不建议以这种方式使用ruby块,只需按照希望Chef执行它们的顺序添加配方。在这种情况下:

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

现在,如果在执行Chef-client期间需要获取某些值,则可以使用延迟评估。 Chef网站上有plenty of information

例如,请检查:

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

最后,如果不需要插入或向文本中添加变量,请尝试使用简单引号' '而不是双" ",也请使用:

"/etc/bowbridge/#{bb_cfg}"

而不是

"/etc/bowbridge/" + bb_cfg