vagrant で ansible 最新環境(on centos7 box)を構築する

# CentOS7 に ansible 環境を構築する

## 目的

- できるだけ最短で構築したい
- 自分用備忘録

## 対象

- ansible 環境を CentOS7 上にとりあえずてっとり早く作りたい方

## 環境

- Mac OS X El Capitan
- VirtualBox
- Vagrant
  - Official CentOS7 Box (7.2)

## リファレンス

- http://docs.ansible.com/ansible/intro_installation.html

## 0. 準備(If needed)

### 0-1. Update box
```shell-session
% vagrant box update --box centos/7
```
### 0-2. ディレクトリの作成と移動

```shell-session
% mkdir -p ~/vagrant/ansible/
% cd ~/vagrant/ansible/
```

## 1. 手動インストールする場合

```shell-session
$ sudo yum -y update

$ sudo yum -y install git python-setuptools libffi-devel python-devel gcc python-crypto openssl-devel

$ sudo easy_install pip

$ sudo pip install --upgrade setuptools

$ sudo -H pip install paramiko PyYAML Jinja2 httplib2 six
```
```shell-session:PIPから
$ sudo pip install ansible
```
```shell-session:GITから
$ git clone git://github.com/ansible/ansible.git --recursive

$ sudo chown -R vagrant: /home/vagrant/ansible

$ echo "source /home/vagrant/ansible/hacking/env-setup > /dev/null" >> /home/vagrant/.bashrc

$ . .bashrc
```
```shell-session:バージョン確認
$ ansible --version
ansible 2.2.0 (devel 70e63ddf6c) last updated 2016/09/15 06:21:30 (GMT +000)
  lib/ansible/modules/core: (detached HEAD 683e5e4d1a) last updated 2016/09/15 06:22:40 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD 8749c40fee) last updated 2016/09/15 06:23:37 (GMT +000)
  config file =
  configured module search path = Default w/o overrides
```

## 2. bootstrap.sh を使って自動でプロビジョニングする
### 2-1. 設定ファイルの準備
```shell-session
$ cat << EOF > Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "centos/7"
  config.vm.provision :shell, path: "bootstrap.sh"
end
EOF
cat Vagrantfile
```
```shell-session:PIPから
$ cat << EOF > bootstrap.sh
#!/usr/bin/env bash

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

sudo yum -y update

sudo yum -y install git python-setuptools libffi-devel python-devel gcc python-crypto openssl-devel
sudo easy_install pip
sudo pip install --upgrade setuptools
sudo -H pip install paramiko PyYAML Jinja2 httplib2 six
sudo pip install ansible
EOF
cat bootstrap.sh
```
```shell-session:GITから
$ cat << EOF > bootstrap.sh
#!/usr/bin/env bash

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

sudo yum -y update

sudo yum -y install git python-setuptools libffi-devel python-devel gcc python-crypto openssl-devel
sudo easy_install pip
sudo -H pip install paramiko PyYAML Jinja2 httplib2 six
git clone git://github.com/ansible/ansible.git --recursive
sudo chown -R vagrant: /home/vagrant/ansible
echo "source /home/vagrant/ansible/hacking/env-setup > /dev/null" >> /home/vagrant/.bashrc
EOF
cat bootstrap.sh
```
### 2-2. VM の起動 と 接続
```shell-session
% vagrant up
% vagrant ssh
```

## 3. Ansible 動作確認
```shell-session
$ which ansible
~/ansible/bin/ansible

$ ansible --version
ansible 2.2.0 (devel 70e63ddf6c) last updated 2016/09/15 07:21:43 (GMT +000)
  lib/ansible/modules/core: (detached HEAD 683e5e4d1a) last updated 2016/09/15 07:22:56 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD 8749c40fee) last updated 2016/09/15 07:23:49 (GMT +000)
  config file =
  configured module search path = Default w/o overrides

$ ansible all -i "localhost," -c local -m shell -a 'date;echo hello world'
localhost | SUCCESS | rc=0 >>
Thu Sep 15 07:25:32 UTC 2016
hello world
```

## 4. おわり