5
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ChefのRole機能、Enviroment機能の使用方法

Posted at

Roleとは

Chefにおけるロールとは、同じ役割のサーバをまとめて管理するための仕組みである。
例えばWebサーバーが5台あった場合、同じ設定をサーバごとに用意しては手間が増えるしミスの可能性も高くなるが、
webというロールを用意して、webロールに対してランリストなどを記述することにより、個別のサーバーへの設定はロールを適用するだけで済むようになる。

Roleの設定方法

Roleの記述

リボジトリの下にあるrolesディレクトリ配下に任意のロール名でjsonファイルを作成する。
Webサーバ用のロールの記述例

roles/web.json
{
  "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ロールが適用される。

nodes/web01.json
{
  "run_list":[
    "role[web]"
  ]
}

Environmentとは

Roleが役割に応じた状態を定義するものに対して、Environmentでは開発環境や本番環境といった環境ごとにAttributeを設定したい場合に使用する機能。

Environmentの設定方法

Environmentの記述

リボジトリの下にあるenvironmentsディレクトリ配下に任意の環境名でjsonファイルを作成する。
開発環境用の記述例

environments/development.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の設定が適用される。

nodes/web01.json
{
  "environment": "develoment",
  "run_list":[
    "role[web]"
  ]
}
5
11
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
5
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?