Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Chef | Notificationで他のリソースに対して任意のアクションをトリガーする

概要

Notificationで他のリソースに対して任意のアクションをトリガーする

詳細

Notificationで他のリソースに対して任意のアクションをトリガーします。

resource "some_resource" do
  # options
  notifies :action, "target_resource"
end

よく利用するシチュエーションとしては、各種設定ファイルを更新した際に
サービスを再起動させる場合などです。

template "some_setting_file.rb" do
  source "some_setting_file.rb"
  owner "root"
  group "root"
  mode "644"
  notifies :restart, "service[some_service]"
end

詳細については公式サイトリンクを参照。
http://docs.opscode.com/resource_common.html#notifications

サンプル

コード

rspec_piccoloをインストールした際にexecute[notification_sample]を呼び出す

execute "notification_sample" do
  command "echo notification!!!!! > /home/vagrant/notification.txt"
end

gem_package "rspec_piccolo" do
  action :install
  version "0.0.3"
  notifies :run, "execute[notification_sample]"
end
実行結果確認
$ pwd
/home/vagrant
$ cat notification.txt
notification!!!!!