Capistranoの実行環境の作成方法1

Capistranoの実行環境について勉強したいけど、うまく環境が作れないという人向けの記事です

前提条件

* 環境はMac
* [VirtualBox](https://www.virtualbox.org/ "VirtualBox")がインストールされている
* [Vagrant](https://www.vagrantup.com/ "Vagrant")がインストールされている

```:bash
mkdir cap_test
```

```:bash
cd cap_test
```

```:bash
vagrant box add "centos_6.6" https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box
```

```:bash
vagrant init
```
```:bash
vi Vagrantfile
```

```:Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.define "host" do |node|
    node.vm.box = "centos_6.6"
    node.vm.hostname = "host"
    node.vm.network :private_network, ip: "192.168.43.61"
  end
  config.vm.define "front1" do |node|
    node.vm.box = "centos_6.6"
    node.vm.hostname = "front1"
    node.vm.network :private_network, ip: "192.168.43.62"
  end
end
```

```:bash
vagrant up
```

vagrantが動いていることを確認
hostとfront1が確認できるはずです

```:bash
vagrant status
```

AnsibleをHostにinstallします

```:bash
vagrant ssh host
```

パスワードはvagrantです

```:bash
su -
```

```:bash
wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

```

```:bash
rpm -Uvh epel-release-6-8.noarch.rpm
```
```:bash
yum install -y ansible
```
sshのkeyを作成します

```:bash
ssh-keygen -t rsa
```
```:bash
vi ~/.ssh/config
```
```:bash
Host front1
  HostName 192.168.43.62
```

```:bash
chmod 600 ~/.ssh/config
```
```:bash
ssh-copy-id front1
```
```:bash
ssh-copy-id localhost
```
```:bash
sudo yum -y update
```

AnsibleをHostで実行します

```:bash
vi hosts
```

```:hosts
[host]
192.168.43.61
[front1]
192.168.43.62
```

疎通確認

```:bash
ansible all -i hosts -m ping
```

試しにマシンスペックを確認

```:bash
ansible all -i hosts -m setup
```

japanというパスワードを暗号化します。
これはAnsibleのファイルに記述されます。
生成されたパスワード sawAfZHeK2qMo

```:bash
python -c 'import crypt; print crypt.crypt("japan", "salt")'
```

```:bash
vi playbook_all.yml
```

```:playbook_all.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: create japan group
      group: name= japan state=present
    - name: add a japan user
      user: name= japan append=yes group= japan groups= japan,wheel password= sawAfZHeK2qMo
    - name: create directory
      file: path=/srv/www state=directory owner=japan group=japan mode=0755
    - name: install libselinux-python
      yum: name=libselinux-python state=latest
    - name: install git
      yum: name=git state=latest
    - name: remove ruby packages
      yum: name={{item}} state=absent
      with_items:
       - ruby
       - ruby-augeas
       - ruby-libs
       - libselinux-ruby
    - name: download ruby
      get_url: url="ftp://ftp.ruby-lang.org/pub/ruby/ruby-2.1.0.tar.gz" dest=/usr/local/src
    - name: tar
      unarchive: src=/usr/local/src/ruby-2.1.0.tar.gz dest=/usr/local/src
    - name: configure
      command: ./configure --prefix=/usr/local/ruby-2.1.0 --disable-install-rdoc --disable-install-capi chdir=/usr/local/src/ruby-2.1.0
    - name: make
      command: make chdir=/usr/local/src/ruby-2.1.0
    - name: make install
      command: make install chdir=/usr/local/src/ruby-2.1.0
    - name: change .bashrc for root
      lineinfile: dest=/root/.bashrc
                  line='PATH=$PATH:$HOME/bin:/usr/local/ruby-2.1.0/bin'
                  backup=yes
                  state=present
    - name: export .bashrc
      lineinfile: dest=/root/.bashrc
                  line='export PATH'
                  state=present
    - name: source .bashrc for root
      shell: source /root/.bashrc
    - name: change .bashrc for japan
      lineinfile: dest=/home/japan/.bashrc
                  line='PATH=$PATH:$HOME/bin:/usr/local/ruby-2.1.0/bin'
                  backup=yes
                  state=present
    - name: export .bashrc
      lineinfile: dest=/home/japan/.bashrc
                  line='export PATH'
                  state=present
    - name: source .bashrc for japan
      shell: source /home/japan/.bashrc
    - name: gem update
      shell: /usr/local/ruby-2.1.0/bin/gem update --system
    - name: gem install bundle
      shell: /usr/local/ruby-2.1.0/bin/gem install bundle
```

```:bash
ansible-playbook -v -i hosts playbook_all.yml
```

```:bash
vi playbook_host.yml
```

```:playbook_host.yml
---
- hosts: front1
  sudo: yes
  tasks:
    - name: install memcached
      yum: name=memcached state=present
    - name: install ImageMagick packages
      yum: name={{item}} state=present
      with_items:
       - ImageMagick
       - ImageMagick-perl
    - name: update mysql rpm
      yum: name="http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm" state=present
    - name: install mysql packages
      yum: name={{item}} state=present
      with_items:
       - mysql
       - mysql-server
       - mysql-devel
    - name: update nginx rpm
      yum: name="http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm" state=present
    - name: install nginx
      yum: name=nginx
    - name: install libxml2 packages
      yum: name={{item}} state=present
      with_items:
       - libxml2
       - libxml2-devel
    - name: install libxslt packages
      yum: name={{item}} state=present
      with_items:
       - libxslt
       - libxslt-devel
    - name: service mysql nginx memcached
      service: name={{item}} state=started enabled=yes
      with_items:
       - mysqld
       - nginx
       - memcached
```

```:bash
ansible-playbook -v -i hosts playbook_host.yml
```

```:bash
vagrant ssh host
```

```:bash
su - 
```

```:bash
visudo
```

コメントアウトを外す

```:visudo
%wheel  ALL=(ALL)       ALL
```

japanユーザーでsshのkeyを作成

```:bash
su - japan
```

```:bash
ssh-keygen -t rsa
```

```bash
vi ~/.ssh/config
```

```:config
Host front1
  HostName 192.168.43.62
```

```:bash
chmod 600 ~/.ssh/config
```

ログインできるようにする
ログインパスワードはjapanです。

```:bash
ssh-copy-id front1
```

```:bash
ssh front1
```