0

我有一个 LWRP 作为其步骤的一部分下载文件,我想用它来指示资源是否已更改

action: install do

  # some other stuff here

  remote_file "/some/file" do
    source node[:mycookbook][:source_file]
    mode 00755
    action :create
    notifies :run, 'ruby_block[set_status]', :immediately
  end

  ruby_block 'set_status' do
    block do
      new_resource.updated_by_last_action(true)
    end
  end
end

在我的食谱中,我有:

 my_provider do
    # configure
    notifies :run, 'something_else', :immediately
 end

remote_file 是否运行似乎并不重要,something_else没有通知,但我不知道为什么。

4

1 回答 1

3

我不确定你可以延迟new_resource.updated_by_last_action使用 ruby​​_block (你试图在你的提供者执行之外运行它?)。由于您的提供程序操作已经在收敛时间运行,因此我通常不会在这里使用 ruby​​ 块。我会做类似的事情:

action: install do

  # some other stuff here

  some_file = remote_file "/some/file" do
    source node[:mycookbook][:source_file]
    mode 00755
    action :nothing
    notifies :run, 'ruby_block[set_status]', :immediately
  end
  some_file.run_action(:create)
  new_resource.updated_by_last_action(true) if some_file.updated_by_last_action?

end

立即调用的另一个好处run_actionremote_file您不再使用 DSL 来创建资源并将其添加remote_file到资源集合中,然后等待厨师在未来某个时间将其收敛(然后等待您的 ruby​​_block 之后) . 您正在此时和那里聚合您关心的资源,并检查它是否已更改(并相应地更新您的自定义资源)。

于 2015-11-06T12:43:31.923 回答