Vagrant + Ansible でCentOSにRubyの環境構築してみる。

## 前提条件

* Mac OS X
* virtualBox + Vagrantがインストール済みであること。

## Vagrantの設定

vagrant box のダウンロード先は以下のサイトから調べる。
http://www.vagrantbox.es/

```bash
# centos6.7をvabrant box に追加する。
$ vagrant box add centos67 https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
# centos用のディレクトリを作成。
$ mkdir -p ~/vagrant/centos67
$ cd ~/vagrant/centos67
$ vagrant init centos67
# 共有用のディレクトリーを作成する。
$ mkdir -p ~/vagrant/centos67/www
```

Vagrantfileを編集する。

```
$ vi Vagrantfile
```

```:Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "centos67"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./www", "/www/"

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
    yum update
  SHELL

  # 2016/03/27追記
  # vagrant up時にplaybookを実行するように追加。
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "play_book.yml"
  end
end
```

## Ansibleの設定ファイル作成

### PlayBookの設定

以下のplay_book.ymlをローカルのVagrantファイルがあるディレクトリと同じ場所に作成する。

```bash
$ vi ~/vagrant/centos67/play_book.yml
```

```yml:play_book.yml
---
- hosts: 127.0.0.1
  connection: local
  sudo: yes
  tasks:
    # Rubyの依存パッケージをinstall
    - yum: name={{item}} state=latest
      with_items:
        - gcc
        - zlib-devel
        - openssl-devel
        - mysql-server
        - mysql-devel
        - libffi-devel
        - libxml2-devel
        - libxslt-devel
        - httpd-devel
        - curl-devel
        - apr-devel
        - apr-util-devel
        - readline-devel
        - ImageMagick
        - ImageMagick-devel

    # Git install
    - yum: name=git state=latest

    # Rubyをrbenv経由でinstall
    - git: >
        repo=https://github.com/sstephenson/rbenv.git
        dest=/home/vagrant/.rbenv/
    - git: >
        repo=https://github.com/sstephenson/ruby-build.git
        dest=/home/vagrant/.rbenv/plugins/ruby-build/
    - file: path=/home/vagrant/.rbenv owner=vagrant state=directory recurse=yes

    - shell: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
      sudo: no
    - shell: echo 'export PATH="$HOME/.rbenv/shims:$PATH"' >> ~/.bash_profile
      sudo: no
    - shell: echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
      sudo: no

    - shell: bash -lc "rbenv install -v 2.2.4"
      sudo: no
    - shell: bash -lc "rbenv rehash"
      sudo: no
    - shell: bash -lc "rbenv global 2.2.4"
      sudo: no
    - shell: bash -lc "gem install bundler"
      sudo: no
```

## サーバセットアップ、Ansibleの実行

```bash
# サーバ起動
$ vagrant up
```

これでサーバの設定とansibleのplaybookが実行され各種インストールが始まります。

```bash

# 以下のように出力される。(ansibleのインストール部分)

PLAY [127.0.0.1] **************************************************************

GATHERING FACTS ***************************************************************
ok: [127.0.0.1]

TASK: [yum name={{item}} state=latest] ****************************************
changed: [127.0.0.1] => (item=gcc,zlib-devel,openssl-devel,mysql-server,mysql-devel,libffi-devel,libxml2-devel,libxslt-devel,httpd-devel,curl-devel,apr-devel,apr-util-devel,readline-devel,ImageMagick,ImageMagick-devel)

TASK: [yum name=git state=latest] *********************************************
changed: [127.0.0.1]

TASK: [git repo=https://github.com/sstephenson/rbenv.git dest=/home/vagrant/.rbenv/
] ***
changed: [127.0.0.1]

TASK: [git repo=https://github.com/sstephenson/ruby-build.git dest=/home/vagrant/.rbenv/plugins/ruby-build/
] ***
changed: [127.0.0.1]

TASK: [file path=/home/vagrant/.rbenv owner=vagrant state=directory recurse=yes] ***
changed: [127.0.0.1]

TASK: [shell echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile] ***
changed: [127.0.0.1]

TASK: [shell echo 'export PATH="$HOME/.rbenv/shims:$PATH"' >> ~/.bash_profile] ***
changed: [127.0.0.1]

TASK: [shell echo 'eval "$(rbenv init -)"' >> ~/.bash_profile] ****************
changed: [127.0.0.1]

TASK: [shell bash -lc "rbenv install -v 2.2.4"] *******************************
changed: [127.0.0.1]

TASK: [shell bash -lc "rbenv rehash"] *****************************************
changed: [127.0.0.1]

TASK: [shell bash -lc "rbenv global 2.2.4"] ***********************************
changed: [127.0.0.1]

TASK: [shell bash -lc "gem install bundler"] **********************************
changed: [127.0.0.1]

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=13   changed=12   unreachable=0    failed=0

```

最後に上記のようなログが書き出されたら無事に終了です。
下記のコマンドでログインし、Rubyのバージョンを確認する。

```bash
# ssh でログイン
$ vagrant ssh
# Rubyのバージョンを確認
$ ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
```

以上、終了！

### 2016/03/27 追記修正しました。