[Vagrant & VirtualBox] Chef: Vagrant から Chef でプロビジョニングする

前回の続き。
vagrant up で実行されるように設定します。

[markdown]
## Project init

前回の復習をかねて、プロジェクトの作成から始めます。

“`prettyprinted
% knife solo init zend
% cd zend
% vagrant init chef/centos-6.5
“`

これでひな型が作成されました。
あわせて .gitignore に .vagrant を追記しておきました。

## vagrant-omnibus をインストールする

プロビジョニングのためには、Vagrant box にも chef がインストールされている必要がありました。
ちなみに chef がインストールされていないと下記のエラーが出ます。

“`prettyprinted
The chef binary (either `chef-solo` or `chef-client`) was not found on
the VM and is required for chef provisioning. Please verify that chef
is installed and that the binary is available on the PATH.
“`

前回、`knife solo prepare` コマンドで box へ chef を手動インストールしていましたが、vagrant plugin で自動化します。

> * [vagrant-omnibusで簡単Chef Client/Chef Soloインストール | Ryuzee.com](http://www.ryuzee.com/contents/blog/6651)

### インストール

プラグインがインストールされていなければ、インストールします。

“`prettyprinted
% vagrant plugin install vagrant-omnibus
“`

### 設定

Vagrantfile に下記を追記します。

“`ruby:Vagrantfile
config.omnibus.chef_version = :latest
“`

ただし、バージョンコントロールしておいた方が良さそうです。

> * [Chef: vagrant-omnibus や knife solo prepare を利用すると wget が 404 error | deadwood](https://www.d-wood.com/blog/2014/09/17_6943.html)

## Cookbook を作成する

前回同様、vim をインストールする Cookbook を作成します。

“`prettyprinted
% knife cookbook create zend -o site-cookbooks
“`

“`ruby:site-cookbooks/zend/recipes/default.rb
package “vim-enhanced” do
action :install
end
“`

“`json:nodes/zend.json
{
“run_list”: [
“recipe[zend]”
]
}
“`

ただし実行した限りでは nodes/zend.json を見ていませんでしたので不要かもしれません。
(削除しても動作します)。

このようなファイル構成となりました。

“`prettyprinted
.
├── Vagrantfile
├── cookbooks
├── data_bags
├── environments
├── nodes
│   └── zend.json
├── roles
└── site-cookbooks
└── zend
├── CHANGELOG.md
├── README.md
├── attributes
├── definitions
├── files
│   └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│   └── default.rb
├── resources
└── templates
└── default
“`

## プロビジョニングとして登録する

前回、`knife solo cook` コマンドで実行していたプロビジョニングを `vagrant up` 時に実行されるように設定を加えます。
Vagrantfile を修正します。

“`ruby:Vagrantfile
VAGRANTFILE_API_VERSION = “2”
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = “chef/centos-6.5”
config.vm.network “private_network”, ip: “192.168.33.10”
config.omnibus.chef_version = :latest
config.vm.provision “chef_solo” do |chef|
chef.cookbooks_path = “./site-cookbooks”
chef.add_recipe “zend”
end
end
“`

> * [入門CHEF SOLO – Qiita](http://qiita.com/k2works/items/29ea5e8db3180f39289c#2-7)

以上で完成しました。

`vagrant up` でプロビジョニングが実行されるようになりました。
[/markdown]