LoginSignup
1
0

More than 5 years have passed since last update.

ChefTips: only_if, not_if は複数使える

Last updated at Posted at 2017-03-16

Chef の Guard機能(only_if, not_if)が 1つのリソース内で複数 指定可能、というお話。
周知の事実なのかもだけど、公式マニュアルには書いてない?ようだったので。

Common Functionality: guard
https://docs.chef.io/resource_common.html#guards

only_if と not_if の混在も可能。
最終的に、一個でもskipしちゃうGuard条件があるとSkipされる

[root@vcent01 ~]# cat <<_EOL_ | chef-apply -s
execute "guard test" do
  user "root"
  command <<-EOM
    echo "execute"
  EOM
  only_if "exit 0"
  only_if "exit 0"
end
_EOL_

Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * execute[guard test] action run
    - execute     echo "execute"
[root@vcent01 ~]# cat <<_EOL_ | chef-apply -s
execute "guard test" do
  user "root"
  command <<-EOM
    echo "execute"
  EOM
  only_if "exit 1" # <- skip
  only_if "exit 0"
end
_EOL_

Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * execute[guard test] action run (skipped due to only_if) <- skipしちゃった

only_ifnot_if の混在の例。

Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * execute[guard test] action run (skipped due to only_if)
[root@vcent01 ~]# cat <<_EOL_ | chef-apply -s
execute "guard test" do
  user "root"
  command <<-EOM
    echo "execute"
  EOM
  only_if "exit 0"  # <- only_if と not_if の混在も可能
  not_if "exit 1"
end
_EOL_

Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * execute[guard test] action run
    - execute     echo "execute"

混在してても最終的には 一個でもskipしちゃうGuard条件があればskip。

[root@vcent01 ~]# cat <<_EOL_ | chef-apply -s
execute "guard test" do
  user "root"
  command <<-EOM
    echo "execute"
  EOM
  not_if "exit 0" # <- skip 
  only_if "exit 0"
end
_EOL_

Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * execute[guard test] action run (skipped due to not_if) <- skipした
1
0
0

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
1
0