Roleとは
Chefにおけるロールとは、同じ役割のサーバをまとめて管理するための仕組みである。
例えばWebサーバーが5台あった場合、同じ設定をサーバごとに用意しては手間が増えるしミスの可能性も高くなるが、
webというロールを用意して、webロールに対してランリストなどを記述することにより、個別のサーバーへの設定はロールを適用するだけで済むようになる。
Roleの設定方法
Roleの記述
リボジトリの下にあるrolesディレクトリ配下に任意のロール名でjsonファイルを作成する。
Webサーバ用のロールの記述例
{
"name": "web",
"chef_type": "role",
"json_class": "Chef::Role",
"default_attributes": {
"apache": {
"listen_ports": [
"80",
"443",
]
},
"override_attributes": {
"apache": {
"max_children": "50"
}
},
"run_list": [
"recipe[git]",
"recipe[apache2]"
]
}
name - 任意のロール名
chef_type - 固定でrole
json_class - 固定でChef::Role
default_attributes - 適用先ノードに定義されていない場合に使用されるAttribute。
override_attributes - 同じAttributeが定義されていても、優先して使用されるAttribute。
run_list: 適用するクックブック
Roleの適用
ノードオブジェクトに以下のように記述するとwebロールが適用される。
{
"run_list":[
"role[web]"
]
}
Environmentとは
Roleが役割に応じた状態を定義するものに対して、Environmentでは開発環境や本番環境といった環境ごとにAttributeを設定したい場合に使用する機能。
Environmentの設定方法
Environmentの記述
リボジトリの下にあるenvironmentsディレクトリ配下に任意の環境名でjsonファイルを作成する。
開発環境用の記述例
{
"name": "development",
"description": "Development environment for xxx service",
"chef_type": "environment",
"json_class": "Chef::Environment",
"default_attributes": {
"apache": {
"max_children": "10"
},
"override_attributes": {
"apache": {
"max_children": "50"
}
},
}
Environmentの適用
ノードオブジェクトに以下のように記述するとdevelopmentの設定が適用される。
{
"environment": "develoment",
"run_list":[
"role[web]"
]
}