LoginSignup
16
16

More than 5 years have passed since last update.

ChefのScript(bash)リソースでflagsを使って例外停止

Last updated at Posted at 2012-12-26

この記事は最終更新から1年以上経過しています。 気をつけてね。

chefのscriptリソースにbashを選ぶと、そのままではスクリプトの途中でエラーが出ても停止しない。

recipe.rb
script "echo_test" do
  interpreter "bash"
  code <<-"EOH"
      ehco
      echo
  EOH
end

Scriptリソースは単にcodeの中身をファイルに書いてbashで実行している、exitコードは最後に実行したコマンドに依存するため、上記レシピではtypoのehcoがあっても続きが実行されてしまう。

flagsを使う

flags "-e"を指定すると、bashに -e ("set -e" と同じ)オプションをつけてくれるので止まる。

recipe_with_exception.rb
script "echo_test" do
  interpreter "bash"
  flags "-e"
  code <<-"EOH"
      ehco
      echo
  EOH
end

実行例

---- Begin output of "bash" -e "/tmp/chef-script20120815-11796-1cn4njv" ----
STDOUT: 
STDERR: /tmp/chef-script20120815-11796-1cn4njv: line 1: ehco: command not found
---- End output of "bash" -e "/tmp/chef-script20120815-11796-1cn4njv" ----
Ran "bash" -e "/tmp/chef-script20120815-11796-1cn4njv" returned 127

ehco: command not found で無事に止まった。

16
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16