---
title: Chef Howto with Vagrant + Berkshelf
tags:  #xtone #chef #vagrant #berkshelf  
author: [NAKAYAMA Masahiro](https://www.docswell.com/user/nekoruri)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/MLJLGPGNER.jpg?width=480
description: Chef Howto with Vagrant + Berkshelf  2013-04-26 Xtone Ltd. ピザ会 Aki / @nekoruri
published: April 26, 13
canonical: https://www.docswell.com/s/nekoruri/58WNLZ-20130426-chef-howto
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/MLJLGPGNER.jpg)

Chef Howto
with Vagrant + Berkshelf
2013-04-26
Xtone Ltd. ピザ会
Aki / @nekoruri


# Page. 2

![Page Image](https://bcdn.docswell.com/page/547MQ4QQJW.jpg)

おさらい
• Chef
– 設定ファイルに書いたとおりにサーバ内を整備
– 新規環境でも既存環境でも結果は同じ
– これで設定ファイルを差分管理できるね！
• 原則
– もうサーバ上で直接作業をしない
• パッケージの追加・更新
• 設定ファイルの変更


# Page. 3

![Page Image](https://bcdn.docswell.com/page/ZP7R8W8KJ9.jpg)

今日のゴール
• 誰でもChefで環境整備ができる
– 新規の案件で、Capistrano走らせるところまで
– Unicornは触らない（Capistranoから操作）


# Page. 4

![Page Image](https://bcdn.docswell.com/page/NPJX8P85EX.jpg)

Chefに欠かせない便利ツール
• Vagrant
– VirtualBox等を良い感じに操作してくれるツール
– Chefを統合して操作できる
– http://www.vagrantup.com/ からダウンロード
• Berkshelf
– BundlerのChef版
– Berksfileに書いたcookbookをGitHubから取得


# Page. 5

![Page Image](https://bcdn.docswell.com/page/63JKKDKRJD.jpg)

おおまかな流れ
1.
2.
3.
4.
5.
6.
Chefレポジトリの新規構築
CookbookをBerkshelfで取得
共通の設定を記述
個別の設定を記述
Vagrantで作成したサーバに適用
本番サーバに適用


# Page. 6

![Page Image](https://bcdn.docswell.com/page/4LE3ZRZGE5.jpg)

1. Chefレポジトリの新規構築
% knife solo init myrepo
% cd myrepo
空のChefレポジトリを作成
% cat &lt;&lt;EOF &gt; .gitignore
/vendor/bundle/
/vendor/cookbooks/
/.bundle/
/bin/
/.vagrant/
EOF
% git init
% git add .
% git commit -m &#039;initialized chef repository&#039;
Gitレポジトリとして初期化
% bundle init
% cat &lt;&lt;EOF &gt; Gemfile
source &#039;https://rubygems.org&#039;
gem &#039;knife-solo&#039;
gem &#039;berkshelf&#039;
EOF
% bundle install --path vendor/bundle --binstubs
% git add Gemfile Gemfile.lock
% git commit -m &#039;installed knife-solo, berkshelf&#039;
chefとberkshelfを導入


# Page. 7

![Page Image](https://bcdn.docswell.com/page/V8EDRYRNJG.jpg)

2. CookbookをBerkshelfで取得
% cat &lt;&lt;EOF &gt; Berksfile
site :opscode
cookbook &#039;apt&#039;
cookbook &#039;chef-solo-search&#039;, git: &#039;git://github.com/edelight/chef-solo-search.git&#039;
cookbook &#039;users&#039;
cookbook &#039;sudo&#039;
利用するcookbookを取得
cookbook &#039;nginx&#039;
cookbook &#039;logrotate&#039;, git: &#039;git://github.com/drewrothstein/logrotate.git&#039;
cookbook &#039;nginx_vhost&#039;, git: &#039;git://github.com/nekoruri/cookbook-nginx_vhost.git&#039;
EOF
% bin/berks install --path vendor/cookbooks
% git add Berksfile Berksfile.lock
% git commit -m &#039;Import apt/users/sudo cookbooks&#039;
% cat &lt;&lt;EOF &gt; solo.rb
file_cache_path
data_bag_path
encrypted_data_bag_secret
cookbook_path
role_path
EOF
&quot;/tmp/chef-solo&quot;
&quot;/tmp/chef-solo/data_bags&quot;
&quot;/tmp/chef-solo/data_bag_key&quot;
[ &quot;/tmp/chef-solo/site-cookbooks&quot;,
&quot;/tmp/chef-solo/vendor/cookbooks&quot; ]
&quot;/tmp/chef-solo/roles&quot;
chef-soloの設定ファイルに
vendor/cookbooksを追加


# Page. 8

![Page Image](https://bcdn.docswell.com/page/4V7PW6WNJ8.jpg)

3. 共通の設定を記述
% bin/knife cookbook create base -o site-cookbooks
% cat &lt;&lt;EOF &gt;&gt; site-cookbooks/base/metadata.rb
depends
&#039;apt&#039;
depends
&#039;users&#039;
depends
&#039;sudo&#039;
EOF
% cat &lt;&lt;EOF &gt;&gt; site-cookbooks/base/recipes/default.rb
include_recipe &#039;apt&#039;
include_recipe &#039;users::sysadmins&#039;
node.override[&#039;authorization&#039;][&#039;sudo&#039;][&#039;passwordless&#039;] = true
include_recipe &#039;sudo&#039;
EOF
% mkdir data_bags/users
% cat &lt;&lt;EOF &gt; data_bags/users/miku.json
{
&quot;id&quot;: &quot;miku&quot;,
&quot;uid&quot;: &quot;10000&quot;,
&quot;comment&quot;: &quot;HATSUNE Miku&quot;,
&quot;ssh_keys&quot;: [
&quot;ssh-rsa AAAA(省略)= miku@client.example.local&quot;
],
&quot;groups&quot;: [ &quot;sysadmin&quot; ],
&quot;shell&quot;: &quot;/usr/bin/zsh&quot;
}
EOF
base cookbookを作成
監視設定等を含む
社内共通のbaseを準備中
管理者ユーザ情報を追加


# Page. 9

![Page Image](https://bcdn.docswell.com/page/L2JV838YJQ.jpg)

4. 個別の設定を記述
% bin/knife cookbook create mysite -o site-cookbooks
% cat &lt;&lt;EOF &gt;&gt; site-cookbooks/mysite/recipes/default.rb
users_manage &#039;app&#039; do
group_id 10001
action :create
end
include_recipe &#039;nginx&#039;
nginx_vhost &#039;static.example.jp&#039;
nginx_vhost &#039;rails.example.jp&#039; do
unicorn true
end
EOF
% cat &lt;&lt;EOF &gt; data_bags/users/app.json
{
&quot;id&quot;: &quot;app&quot;,
&quot;ssh_keys&quot;: [
&quot;ssh-rsa AAAA(省略) = usera@a.example.jp&quot;,
&quot;ssh-rsa AAAA(省略) = userb@b.example.jp&quot;
],
&quot;groups&quot;: [ &quot;app&quot; ],
&quot;shell&quot;: &quot;/usr/bin/zsh&quot;
}
EOF
ウェブサイトの設定を
mysite cookbookとして作成
デプロイ用ユーザの情報を設定


# Page. 10

![Page Image](https://bcdn.docswell.com/page/Z5EG535WEL.jpg)

4. 個別の設定を記述
% vim site-cookbooks/mysite/templates/default/static.example.jp.erb
% vim site-cookbooks/mysite/templates/default/rails.example.jp.erb
% cat &lt;&lt;EOF &gt; roles/web.rb
name &quot;web&quot;
description &quot;Web Server&quot;
run_list(
&quot;recipe[base]&quot;,
&quot;recipe[mysite]&quot;
)
EOF
% git add .
% git commit -m &#039;mysite configuration&#039;
nginxの
設定ファイルを作成
baseとmysiteを呼ぶRoleを作成
ここまでの設定をcommit


# Page. 11

![Page Image](https://bcdn.docswell.com/page/V4JQZ5ZQJP.jpg)

5. Vagrantで作成したサーバに適用
% vagrant init precise32 http://files.vagrantup.com/precise32.box
サーバ準備
% vi Vagrantfile
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = [&quot;vendor/cookbooks&quot;, &quot;site-cookbooks&quot;]
chef.roles_path = &#039;roles&#039;
chef.data_bags_path = &#039;data_bags&#039;
chef.add_role(&#039;web&#039;)
chef.json = {
&quot;authorization&quot; =&gt; {
&quot;sudo&quot; =&gt; {
&quot;users&quot; =&gt; [ &quot;vagrant&quot; ]
}
}
}
end
% git add Vagrantfile
Vagrantの設定
（Vagrant.configure block）
Role &quot;Web&quot;を適用
vagrantユーザにsudo許可
% git commit &#039;Initialized vagrant&#039;
% vagrant up
% vagrant provision
サーバを起動してChefの設定を適用
サーバ起動後にChefの設定を再適用


# Page. 12

![Page Image](https://bcdn.docswell.com/page/DK74393YE1.jpg)

6. 本番サーバに適用
• 前回紹介した手順と同じ
% knife solo prepare root@192.0.2.100
% vi nodes/192.0.2.100.json
% knife solo cook root@192.0.2.100


