CentOS7 に ansible 環境を構築する
目的
- できるだけ最短で構築したい
- 自分用備忘録
対象
- ansible 環境を CentOS7 上にとりあえずてっとり早く作りたい方
環境
- Mac OS X El Capitan
- VirtualBox
- Vagrant
- Official CentOS7 Box (7.2)
リファレンス
0. 準備(If needed)
0-1. Update box
% vagrant box update --box centos/7
0-2. ディレクトリの作成と移動
% mkdir -p ~/vagrant/ansible/
% cd ~/vagrant/ansible/
1. 手動インストールする場合
$ 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
PIPから
$ sudo pip install ansible
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
バージョン確認
$ 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. 設定ファイルの準備
$ cat << EOF > Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.provision :shell, path: "bootstrap.sh"
end
EOF
cat Vagrantfile
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
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 の起動 と 接続
% vagrant up
% vagrant ssh
3. Ansible 動作確認
$ 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