おっさんエンジニアの忘備録

Linux系の各種作業を自分用の忘備録として残しています

Vagrant + Chefでの環境構築自動化

Vagrantで仮想環境の構築を行うタイミングで、環境構築も一緒に出来るようにします。
今回の例ではnginxをインストール&起動するようにしています。

環境

◆ホストOS(RHEL6.7)
◆ゲストOS(CentOS6.7)
◆Chef: 12.9.38 ※既にVagrantに追加している「centos67」というBOXを利用します

検証用のディレクトリ作成

今回は「/root/chef-test」ディレクトリを作成しています。

# cd
# mkdir chef-test

この中にVagrantfileやchefの各ファイルを格納することとします。

Gemfile作成

# cd /root/chef-test
# vi Gemfile
source 'https://rubygems.org'

gem 'chef'
gem 'knife-solo'
gem 'berkshelf'

Gemのインストール

# cd /root/chef-test
# bundle install
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your
bundle as root will break this application for all non-root users on this machine.
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies.....
Using addressable 2.4.0
Using multipart-post 2.0.0
Using httpclient 2.7.2
Using buff-extensions 1.0.0
Installing hashie 3.4.4
Using buff-ignore 1.1.1
Using buff-ruby_engine 0.1.0
Installing hitimes 1.2.4 with native extensions
Using nio4r 1.2.1
Using fuzzyurl 0.8.0
Using mixlib-config 2.2.1
Using mixlib-shellout 2.2.6
Using erubis 2.7.0
Using json 1.8.3
Using mixlib-log 1.6.0
Using rspec-support 3.4.1
Using diff-lcs 1.2.5
Using retryable 2.0.3
Using semverse 1.2.1
Using cleanroom 1.0.0
Using minitar 0.5.4
Installing molinillo 0.4.5
Using thor 0.19.1
Using builder 3.2.2
Using bundler 1.11.2
Using libyajl2 1.2.0
Using rack 1.6.4
Using uuidtools 2.1.5
Using highline 1.7.8
Installing mixlib-cli 1.6.0
Using net-ssh 2.9.4
Using ffi 1.9.10
Using ipaddress 0.8.3
Using plist 3.2.0
Using systemu 2.6.5
Using wmi-lite 1.0.0
Using proxifier 1.0.3
Installing multi_json 1.12.0
Using net-telnet 0.1.1
Using sfl 2.2
Using syslog-logger 1.6.8
Using faraday 0.9.2
Using varia_model 0.4.1
Using buff-shell_out 0.2.0
Using timers 4.0.4
Installing chef-config 12.9.41
Using rspec-core 3.4.4
Using rspec-expectations 3.4.0
Using rspec-mocks 3.4.1
Using solve 2.0.3
Using ffi-yajl 2.2.3
Using net-sftp 2.1.2
Using net-ssh-gateway 1.2.0
Using net-scp 1.2.1
Using sawyer 0.7.0
Using buff-config 1.0.1
Using celluloid 0.16.0
Using rspec_junit_formatter 0.2.3
Using rspec-its 1.2.0
Using mixlib-authentication 1.4.0
Using rspec 3.4.0
Installing chef-zero 4.6.2
Installing ohai 8.15.1
Using net-ssh-multi 1.2.1
Installing specinfra 2.57.2
Using octokit 4.3.0
Using celluloid-io 0.16.2
Installing serverspec 2.34.0
Using ridley 4.5.0
Installing chef 12.9.41
Using berkshelf-api-client 2.0.2
Using knife-solo 0.5.1
Using berkshelf 4.3.2
Bundle complete! 3 Gemfile dependencies, 73 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
# ls -l
合計 12
-rw-r--r-- 1 root root   76  5月  6 13:33 2016 Gemfile
-rw-r--r-- 1 root root 5219  5月  6 13:37 2016 Gemfile.lock

vagrant 初期化(Vagrantfileの作成)

# cd /root/chef-test
# vagrant init centos67
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
[root@124x39x29x22 chef-test]# ls -l
合計 16
-rw-r--r-- 1 root root   76  5月  6 13:33 2016 Gemfile
-rw-r--r-- 1 root root 5219  5月  6 13:37 2016 Gemfile.lock
-rw-r--r-- 1 root root 3020  5月  6 13:45 2016 Vagrantfile

nginx用のクックブック作成

# cd /root/chef-test
# bundle exec knife cookbook create nginx -o ./site-cookbooks
** Creating cookbook nginx in /root/chef-test/site-cookbooks
** Creating README for cookbook: nginx
** Creating CHANGELOG for cookbook: nginx
** Creating metadata for cookbook: nginx
# ls -l
合計 24
-rw-r--r-- 1 root root   84  5月  6 14:22 2016 Berksfile
-rw-r--r-- 1 root root   76  5月  6 13:33 2016 Gemfile
-rw-r--r-- 1 root root 5219  5月  6 13:37 2016 Gemfile.lock
-rw-r--r-- 1 root root 3079  5月  6 13:58 2016 Vagrantfile
drwxr-xr-x 4 root root 4096  5月  6 14:29 2016 site-cookbooks
# cd site-cookbooks/
# ls -l
合計 8
drwxr-xr-x 10 root root 4096  5月  6 14:29 2016 nginx

nginx用のレシピ作成

# vi /root/chef-test/site-cookbooks/nginx/recipes/default.rb
#
# Cookbook Name:: nginx
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

include_recipe "yum-epel"

package "nginx" do
    action :install
end

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

コンフィグの内容は以下の通りです。
(1)「yum-epel」より「nginx」をインストール
(2)「nginx」を自動起動
(3)「nginx」を起動
(4)「nginx」のサービスに関して「status」「restart」「reload」を有効とする

クックブックの依存関係の定義(Berksfileの編集)

# vi /root/chef-test/Berksfile
site :opscode

cookbook "yum-epel"
cookbook "nginx", path: "./site-cookbooks/nginx"

yum-epel」は「cookbooks」ディレクトリ内のレシピを使い、nginxは「site-cookbooks」ディレクトリ内のレシピを使用します。

クックブックのダウンロード

# cd /root/chef-test
# berks vendor ./cookbooks
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Fetching 'nginx' from source at site-cookbooks/nginx
Fetching cookbook index from https://supermarket.chef.io...
Using yum-epel (0.7.0)
Using yum (3.10.0)
Using nginx (0.1.0) from source at site-cookbooks/nginx
Vendoring nginx (0.1.0) to ./cookbooks/nginx
Vendoring yum (3.10.0) to ./cookbooks/yum
Vendoring yum-epel (0.7.0) to ./cookbooks/yum-epel
# ls -l
合計 32
-rw-r--r-- 1 root root   84  5月  6 14:22 2016 Berksfile
-rw------- 1 root root  139  5月  6 14:31 2016 Berksfile.lock
-rw-r--r-- 1 root root   76  5月  6 13:33 2016 Gemfile
-rw-r--r-- 1 root root 5219  5月  6 13:37 2016 Gemfile.lock
-rw-r--r-- 1 root root 3079  5月  6 13:58 2016 Vagrantfile
drwxr-xr-x 5 root root 4096  5月  6 14:31 2016 cookbooks
drwxr-xr-x 4 root root 4096  5月  6 14:29 2016 site-cookbooks
# cd cookbooks/
# ls -l
合計 12
drwxr-xr-x 10 root root 4096  5月  6 14:31 2016 nginx
drwxr-xr-x  8 root root 4096  5月  6 14:31 2016 yum
drwxr-xr-x  4 root root 4096  5月  6 14:31 2016 yum-epel

「cookbooks」ディレクトリ内にクックブックがコピーされます。

vagrant-omnibusプラグインのインストール

Vagrantを起動するときにChef Client/Chef Soloがインストールされていなければ自動でインストールさせるため、プラグインをインストールします。

# cd /root/chef-test
# vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...
Installed the plugin 'vagrant-omnibus (1.4.1)'!

Vagrantfile修正

# vi /root/chef-test/Vagrantfile
Vagrant.configure(2) do |config|
   ・
   ・
  # カスタマイズ用
  # IPアドレスの設定
  config.vm.network "private_network", ip: "192.168.33.13"

  # ポートフォワードの設定
  config.vm.network "forwarded_port", guest: 80, host: 8888

  # vagrant-omnibusの有効化
  config.omnibus.chef_version = :latest

  # プロビジョニング
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
    chef.run_list = %w[
      recipe[yum-epel]
      recipe[nginx]
    ]
  end
end

赤字の箇所を追記します。

vagrant起動

# cd /root/chef-test
# vagrant up --provision

ブラウザで確認

f:id:saosao_0706:20160509174243p:plain
正常に構築が完了していれば上記のような画面が表示されます。