LoginSignup
38

More than 5 years have passed since last update.

chefのJSON設定覚書

Last updated at Posted at 2014-11-05

nodes/, roles/, environments/ でJSONの属性名とか微妙に違うので、公式サンプルからゆるくテンプレ化したい。
埋めるべき項目がわかんないと、結構迷うので。ということで個人用メモとして残しておきます。

概要

  • run_list : 実行するレシピ
  • attributes : レシピに適用する属性値

この2つを決めるための定義ファイルが nodes/, roles/, environments/ から呼び出される。

  • nodes/<hostname>.json : ホストごとの設定
  • roles/<rolename>.json : 役割(role)別の設定。利用に際して初心者Chefアンチパターンを読むことをおすすめする。
  • environments/<envname>.json : environmentの設定を書く。開発用や本番用などの環境で分けて、同じ環境内の複数のホストで共通の設定(DBの接続先など)があればここに書くと便利。

chef_typejson_class も記載したが、固定の情報ではあるので無くても動く。が、万が一rolesのテンプレからnodesにコピーしたりした時にわかりにくいので、コピペ用に書いておく。

nodes

参考1 : https://docs.getchef.com/knife_node.html
参考2 : https://github.com/opscode/chef/blob/master/lib/chef/node.rb

nodes/sample.json
{
    "name": "sample",
    "chef_type": "node",
    "json_class": "Chef::Node",
    "chef_environment" : "sample_env",
    "run_list": [
        "recipe[hoge]",
        "role[foo]"
    ],
    "default": {
    },
    "normal": {
    },
    "override": {
    },
    "automatic": {
    }
}
  • "chef_environment"environment が指定できる。指定がない場合は _default になる。
  • "automatic" は ohai で生成されるサーバ内のデータだから基本的に書くことはない

roles

参考1 : https://docs.getchef.com/knife_role.html
参考2 : https://github.com/opscode/chef/blob/master/lib/chef/role.rb

roles/role1.json
{
    "name": "role1",
    "description": "Description for Role",
    "chef_type": "role",
    "json_class": "Chef::Role",
    "run_list": [
        "recipe[cookbook_name::recipe_name]",
        "role[role_name]"
    ],
    "default_attributes": {
    },
    "override_attributes": {
    }
}
  • 別の role を参照することができるっぽい?

environments

参考1 : https://docs.getchef.com/knife_environment.html
参考2 : https://github.com/opscode/chef/blob/master/lib/chef/environment.rb

ドキュメントの方にJSONサンプルがないでござる。

environments/sample.json
{
    "name": "sample",
    "description": "サンプル環境",
    "chef_type": "environment",
    "json_class": "Chef::Environment",
    "default_attributes": {
    },
    "override_attributes": {
    },
    "cookbook_versions" : {
        "rabbitmq", "= 3.2.2"
    }
}
  • run_list は書けない
  • cookbook_versions で利用するcookbookのバージョンを指定できるらしい

data_bags

参考 : https://docs.getchef.com/essentials_data_bags.html

ついでに data_bags についてメモ。これも利用に際して初心者Chefアンチパターンを読むことをおすすめする。

data_bags/<data_bag_name>/<data_bag_item_name>.json に定義を書いて、レシピの中から利用する。
利用すると言っても上記の属性値のように自動でロードされるわけではなく、Rubyコードで読みに行く。

data_bags/users/hoge.json
{
    "id": "hoge",
    "password": "$1$YY0QI8p.$6zBvRUgjIBhF5Q7b26sSI1"
}
data_bag("users").each { |id|
  u = data_bag_item("users", id)
  user id do
    password u["password"]
  end
}

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
38