LoginSignup
1

More than 5 years have passed since last update.

chefの配列attributeがdeepmergeされる対策

Last updated at Posted at 2014-12-19

roleでは値を指定していて、environmentでは値を空にしたいというケースがありました。
(具体的に言うと、webサーバのロールではfluentdでログを飛ばすようにしたいがsandbox的なenvironmentではログは全止めにしたかった)
で、値を設定しているattributeが配列だった+roleでもenvironmentでもoverride_attributeにattributeを書いていました。
よってenvironmentで空にしたかったが配列attributeでdeepmergeで既存の値はそのままで空の値が追加されてしまった。(そしてログは飛んでしまった)

結果として、role側(上書きしたい側)のattributeをdefault_attributeに移動したら解決しました。

以下サイトの3.3あたりにdeepmergeのことが書かれていました。
http://metasearch.sourceforge.jp/wiki/index.php?ChefTips

以下サイトはattributeの優先順位についてくわしく書かれていました。
http://dev.classmethod.jp/server-side/chef/attribute-overrides-pattern/

以下のようにしておくと、environmentで指定したとおり配列アトリビュートが上書きされました。

environment側

  "override_attributes": {
        "fluentd": {
                "client_conf_types": []
        },

role側

  "default_attributes": {
        "fluentd": {
                "client_conf_types": ["tomcat", "httpd"]
        },

配列attributeを上書きする(DeepMergeされないようにする)には、
上書きされるほうをdefault_attributesに書き、上書きするほうをoverride_attributesに書くといい
ようです。
勉強になりました!

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