Vagrant で仮想マシンを構築する(3) 仮想マシンへのプロビジョニング(chef solo)

Vagrant プロビジョニングの chef 編です。
shell script でプロビジョニングした nginx を chef solo で行います。

chef zero とかありますが、chef solo でいきます。

仮想マシン起動

$ mkdir -p vagrant_example
$ cd vagrant_example
$ vagrant init ubuntu/trusty64
$ vagrant up

vagrant-omnibus のインストールと設定

仮想マシン起動時に chef をインストールしてくれるプラグイン vagrant-omnibus をインストールします。

$ vagrant plugin install vagrant-omnibus
$ vagrant plugin list
vagrant-omnibus (1.4.1)
…..

自動で最新バージョンをインストールするように設定します。

$ vi Vagrantfile
Vagrant.configure(2) do |config|
  config.omnibus.chef_version = :latest
end

仮想マシンを再起動します。

$ vagrant reload –provision

プロビジョニング用 chef recipe の作成

@local host
$ mkdir -p cookbooks/nginx/recipes
$ vi cookbooks/nginx/recipes/default.rb
execute 'aptitude update' do
  command 'aptitude update'
end

package 'nginx' do
  action :install
end

execute 'rm -rf /usr/share/nginx/html'
link '/usr/share/nginx/html' do
  to '/home/vagrant/data'
end

service 'nginx' do
  supports :restart => true, :reload => true
  action [ :enable, :start]
end

Vagrantfile 設定

$ vi Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 80, host: 8888
  config.vm.synced_folder "./data", "/home/vagrant/data"
  config.omnibus.chef_version = :latest
  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = "./cookbooks/"
    chef.add_recipe "nginx"
  end
end

仮想マシンを再起動します。

$ vagrant reload –provision

仮想マシンに nginx がインストールされることを確認します。

vagrant provision - Command-Line Interface - Vagrant Documentation
Chef Solo - Provisioning - Vagrant Documentation