在Chef中,如何通知资源之外的资源



我想创建一个execute资源,它设置一些环境变量并调用某个命令。但是这个execute资源应该只是一种"方法",即定义一些我以后可以重用的功能。我希望能够从其他食谱触发这个execute,目前我正在使用ruby_block,虽然我有一种感觉我做错了什么。(我的烹饪书叫commands)

这是我的可重用功能:

# default.rb
execute 'Run command' do
  Chef::Log.info("Running command: #{node['task']}")
  command node['task']
  cwd deploy[:current_path]
  environment (node[:task] || {})
  action :nothing
  not_if node[:task].nil?
end

这是使用它的特定任务之一:

# rake_db_migrate.rb
include_recipe 'commands'
node.set[:task] = 'rake db:migrate'
ruby_block 'Trigger command' do
  block {}
  notifies :run, 'execute[Run command]', :immediately    
end

我还希望能够通过在节点上设置属性来运行任意任务,这本质上与上述相同,但不写入node['task']:

# run.rb
include_recipe 'commands'
ruby_block 'Trigger command' do
  block {}
  notifies :run, 'execute[Run command]', :immediately    
end

本质上我想在default.rb中定义一些可重用的功能,然后能够从其他食谱中使用这个,如run.rbrake_db_migrate.rb,这是正确的方式还是我错过了什么?

在这种情况下,您可能需要使用定义:http://docs.chef.io/definitions.html

定义可重用位并使用'task'作为参数

最新更新