しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

Itamae入門した ~ Vagrantのサーバーにnginx入れるまで

Itamaeを使ってみたので使い方やインストール方法をメモ。

Itamaeとは

@ryot_a_raiさんの制作したサーバー構成管理ツールです。
Chef-Soloをよりシンプルにしたようなツールです。

github.com

インストール方法

gem install itamae

ItamaeでHello world

まずはtest_recipe.rbを作って下のようにレシピを書きます。

execute "echo HelloWorld!" do
  command "echo HelloWorld"
end

次は下コマンドでItamaeを実行します。
HelloWorldが下から3行目に出力されている事が分かるかと思います。
-lはログレベルのオプションです。

itamae_test$ itamae local test_recipe.rb -l debug
 INFO : Starting Itamae...
DEBUG : Executing `mkdir -p /tmp/itamae_tmp`...
DEBUG :   exited with 0
DEBUG : Executing `chmod 777 /tmp/itamae_tmp`...
DEBUG :   exited with 0
 INFO : Recipe: /Users/sugimoto/Desktop/itamae_test/test_recipe.rb
DEBUG :   execute[echo HelloWorld!]
DEBUG :     execute[echo HelloWorld!] action: run
DEBUG :       (in pre_action)
DEBUG :       (in set_current_attributes)
DEBUG :       (in show_differences)
 INFO :       execute[echo HelloWorld!] executed will change from 'false' to 'true'
DEBUG :       Executing `echo HelloWorld`...
DEBUG :         exited with 0
DEBUG :         stdout | HelloWorld
DEBUG :       This resource is updated.
DEBUG :       This resource is updated.

ItamaeをVagrantで実行

Vagrantfileを作って下のように記述します。

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "shimesaba"
  config.vm.box_url = "https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box"
  config.vm.network "private_network", ip: "192.168.33.11"
end

終わったらサーバーを起動します。

vagrant up

test_recipe.rbも少し修正します。

execute "echo HelloWorld!" do
  command "touch itamae_ok"
end

Itamaeを実行すると、サーバーのホームディレクトリにitamae_okファイルが生成されているかと思います。

itamae ssh --vagrant test_recipe.rb

Itamaeでnginxをインストール

test_recipe.rbを下のように書き換えます。

package 'http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm' do
  not_if 'rpm -q nginx-release-centos-6-0.el6.ngx.noarch'
end

package "nginx" do
  action :install
end

Itamaeを実行すればnginxがインストールされるはずです。

itamae ssh --vagrant test_recipe.rb

その他のコマンド

下記事に情報がまとまっているのでこちらを見れば大体の事ができそうです。
Itamaeチートシート - Qiita

参考URL

ItamaeとVagrantでruby環境を構築する - Qiita
Itamae - Infra as Code 現状確認会 // Speaker Deck
itamae を使って CentOS6.5 に MySQL をインストールする時の注意 - Qiita