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

时间:2014-03-27 10:31:38

标签: chef chef-recipe

我想创建一个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,这是正确的方法还是我遗失了什么?

1 个答案:

答案 0 :(得分:2)

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

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

相关问题