カタカタブログ

SIerで働くITエンジニアがカタカタした記録を残す技術ブログ。Java, Oracle Database, Linuxが中心です。たまに数学やデータ分析なども。

Macにitamaeを入れて、Vagrant仮想マシンのプロビジョニングをしてみる

以前、Chefを使ってVagrantの仮想マシンをプロビジョニングする方法を検証したが、かなり複雑だった。

今回、軽量Chefという位置づけのitamaeというツールが最近熱いらしく、試してみることにした。

環境情報

当環境の情報は以下の通り。

  • ホストOS: Mac OS X Yosemite (10.10.2)
  • ホスト機のRuby : 2.1.5
  • ゲストOS: Cent OS 7.0
  • Virtual Box : 4.3.26
  • Vagrant : 1.7.2
  • itamae : 1.2.11 (今回インストール)

参照するドキュメントは以下。
https://github.com/itamae-kitchen/itamae/wiki/Getting-Started

macにitamaeインストール

itamae用ディレクトリ作成を適当に場所に作成する。
ここをitamae作業用のトップディレクトリとして、レシピ等をまとめておくことにする。

$ mkdir itamae
$ cd itamae/

Gemでitamaeインストール
itamaeはGemでインストールできるので、Gemfileを用意する。

$ bundle init
Writing new Gemfile to /var/lib/itamae/Gemfile

Gemfileの中身は以下。

source "https://rubygems.org"

gem 'itamae'
gem 'rake'
gem 'serverspec'

bundle install

$ bundle install --path=vendor/bundle
Fetching gem metadata from https://rubygems.org/.......
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...Installing rake 10.4.2Installing ansi 1.5.0Installing diff-lcs 1.2.5Installing hashie 3.4.1Installing schash 0.1.1Installing net-ssh 2.9.2Installing net-scp 1.2.1Installing specinfra 2.28.0Installing thor 0.19.1Installing itamae 1.2.11Installing multi_json 1.11.0Installing rspec-support 3.2.2Installing rspec-core 3.2.2Installing rspec-expectations 3.2.0Installing rspec-mocks 3.2.1Installing rspec 3.2.0Installing rspec-its 1.2.0Installing serverspec 2.14.0
Using bundler 1.8.2Bundle complete! 3 Gemfile dependencies, 19 gems now installed.
Bundled gems are installed into ./vendor/bundle.

itamaeをbundle execで実行できることを確認する

$ bundle exec itamae version
Itamae v1.2.11

itamae検証用のVagrant仮想マシン作成

これは前作ったVagrantを使ってCent OS7のマシンを作成する。今回はここの詳細は割愛する。
とりあえず仮想マシンのホスト名を”vfitamae”とした。

$ vagrant up
$ vagrant ssh
Last login: Fri Aug  1 09:45:51 2014 from 10.0.2.2
[vagrant@vfitamae ~]$ 

itamaeレシピ作成

今回も例としてapacheをインストールして起動させてみる。なおitamaeは仮想マシン内で実行するのではなく、mac側で開発し、開発したレシピをリモート(ssh)で仮想マシン内で実行させるという方式をとる。

itamae作業ディレクトリに戻り、レシピを作成する。

$ mkdir recipes
$ vi recipes/first_recipe.rb

以下のようなレシピをサンプルとして作成した。

first_recipe.rb

# firewalld停止,無効化
service 'firewalld' do
  action [:disable, :stop]
end

# SELinux無効化
execute 'disable SELinux' do
  user "root"
  command <<-EOS
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
    setenforce 0
EOS
end

# Apacheインストール
package 'httpd' do
  action :install
end

# Apache起動、有効化
service 'httpd' do
  action [:enable, :start]
end

# index.html配置
remote_file "/var/www/html/index.html" do
  owner "root"
  group "root"
  source "remote_files/index.html"
end

このレシピは以下の処理を行う。

  • firewallを停止・無効化
  • SELinuxの無効化
  • httpdのインストールと起動
  • index.htmlを/var/www/htmlに配置するレシピ。

※ itamaeのremote_filesは最初/tmpに配置するようで、SELinuxが有効化されていると/var/www/htmlに移動してもラベルが付与されていないためエラーとなる。詳しくは別記事書いたので、興味があればこちらを参照のこと。
ApacheでSELinuxが原因で403 Forbiddenエラー - カタカタブログ

また、apacheで画面表示するためのindex.htmlは以下に配置しておく。

$ mkdir recipes/remote_files
$ vi recipes/remote_files/index.html

index.html

Hello, itamae!

itamae実行

itamaeを実行前に、仮想マシンにhttpdがインストールされていないことを確認しておく。

[root@vfitamae ~]# systemctl status httpd
httpd.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

確かにhttpdは動いていない。

では、itamaeを実行する。
vagrantオプションを使えばよいのだが、今回itamae用のディレクトリをVagrantディレクトリと分けて作ったので、sshで直接ログインする。面倒なので、鍵も使わずに直接ユーザ名とパスワードを指定してvagrant/vagrantで入る。

$ bundle exec itamae ssh -h vfitamae -u vagrant recipes/first_recipe.rb
 INFO : Starting Itamae...
Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
vagrant@vfitamae's password:vagrant
 INFO : Recipe: /var/lib/itamae/recipes/first_recipe.rb
 INFO :   service[firewalld] enabled will change from 'true' to 'false'
 INFO :   service[firewalld] running will change from 'true' to 'false'
 INFO :   execute[disable SELinux] executed will change from 'false' to 'true'
 INFO :   package[httpd] installed will change from 'false' to 'true'
 INFO :   service[httpd] enabled will change from 'false' to 'true'
 INFO :   service[httpd] running will change from 'false' to 'true'
 INFO :   remote_file[/var/www/html/index.html] exist will change from 'false' to 'true'
 INFO :   remote_file[/var/www/html/index.html] owner will be 'root'
 INFO :   remote_file[/var/www/html/index.html] group will be 'root'

成功したよう。

仮想マシンにログインし、apacheがインストールされたか確認する。

[root@vfitamae ~]# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active:active (running)since Sun 2015-04-05 03:51:04 EDT; 26s ago
 Main PID: 8223 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─8223 /usr/sbin/httpd -DFOREGROUND
           ├─8224 /usr/sbin/httpd -DFOREGROUND
           ├─8225 /usr/sbin/httpd -DFOREGROUND
           ├─8226 /usr/sbin/httpd -DFOREGROUND
           ├─8227 /usr/sbin/httpd -DFOREGROUND
           └─8228 /usr/sbin/httpd -DFOREGROUND
Apr 05 03:51:04 vfitamae httpd[8223]: AH00558: httpd: Could not reliably determine the ...age
Apr 05 03:51:04 vfitamae systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

apacheが起動している!
さらに、index.htmlも配置されている

[root@vfitamae ~]# ls -l /var/www/html/index.html
-rw-r--r--. 1 root root 15 Apr  5 05:59 /var/www/html/index.html
[root@vfitamae ~]# cat /var/www/html/index.html
Hello, itamae!

Apacheが動いているか確認する。
macから以下のURLにアクセスする(あらかじめvfitamaeのIPアドレスをmacのhostsに記載しておく)。
http://vfitamae/index.html
f:id:osn_th:20150405212337p:plain
正しく画面が表示される!レシピはうまく動いたよう。

まとめ

Chefと比べると、だいぶ簡単にプロビジョニングができる印象。しかもレシピの書き方はChefを踏襲しているため、Chefからの移行として使うには、特に抵抗なく受け入れることができる。Chefはサーバの数が多い時は優位なのだろうが、Vagrantでのローカル仮想マシンを使う場合など、サーバの数が少ない用途の場合はitamaeは十分強力な機能を持っていると感じた。どこまでitamaeでできるのかをもう少し検証してみたい。

今回はここまで。

関連記事