5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vagrant環境構築

Posted at

ドットインストールより

##手順
1.virtualBoxのインストール
2.vagrantのインストール
(どちらも公式のインストーラを利用)
3.Boxの作成(オフライン環境でも仮想環境を作成したい場合のみ)
4.仮想環境の定義ファイルの作成とipの設定
5.必要なアプリのインストール

##用語
###bentoとは
chef社が公開しているvagrant用のOSイメージ(box)
###boxとは
仮想マシンを作成する際に必要なOSのディスクイメージファイル等が入ったファイルの集まり

##チートシート

$ cd vagrant 
$ mkdir centOS
$ cd centOS
$ vagrant init centos/7

# 下はvagrantfileの該当箇所を直接案コメントしてもよい
$ sed -i '' -e 's/# config.vm.network "private_network", ip: "192.168.33.10"/config.vm.network "private_network", ip: "192.168.33.10"/' Vagrantfile

# これでcentOSに入れる
$ vagrant ssh

# ubuntu のapt的なやつ
$ sudo yum -y update

# git のインストール
$ sudo yum install git

# ドットインストールでAnsibleのスクリプトを書いていてくれるのでそちらを利用
$ git clone https://github.com/dotinstallres/centos6.git
$ cd centos6
$ ./run.sh

トラブルシュート

ドットインストールの作ってくれたansibleスクリプトを実行すると出るエラー
Could not find the requested service iptables: host

fatal: [xxx.xxx.xxx.xxx]: FAILED! => {"changed": false, "failed": true, "msg": "Could not find the requested service iptables: host"}

これはcentOSの7を使っている出るエラー
RHEL7以降だとiptablesというサービスが存在しないためこうなる。RHEL6なら発生しないはず。
RHEL7以降ではfirewalldというサービスを使っている。
main.ymlを見るとタスクが[disable iptables]となっている。つまり無効化できればいいはず。
なのでfirewalldを無効化すれば問題ないはず。

$ systemctl stop firewalld
$ systemctl disable firewalld
# rootのパスワードは通常vagrant

そしてmain.ymlの[disable iptables]をコメントアウトしておく。

$ vi main.yml
# #でコメントアウト tasks以下のdisable iptables

##Vagrantコマンドメモ

# 今あるBoxの一覧を表示する
$ vagrant box list

# ローカルにBoxをインストールする
$ vagrant add [box-name] [box-url]

# 定義ファイルの作成
$ vagrant init [box-name] [box-url]

##centos6の中身

run.sh
test -z $(which ansible-playbook) && sudo yum install -y epel-release && sudo yum install -y ansible
sudo yum update -y ansible
ansible-playbook main.yml --connection=local
main.yml
---
- hosts: localhost
  become: yes
  vars:
    - ruby_version: 2.3.1
    - python_version: 3.5.2
  tasks:
#いらない
#    - name: disable iptables
#      service: name=iptables state=stopped enabled=no
    - name: install libselinux-python
      yum: name=libselinux-python state=latest
    - name: Disabled SELinux
      selinux:
        state: disabled
    - name: remove localtime
      file: path=/etc/localtime state=absent
    - name: change timezone
      file: src=/usr/share/zoneinfo/Asia/Tokyo dest=/etc/localtime state=link force=yes mode=0644
# dest=/etc/sysconfig/i18nの部分はエラーになる。
# centos7では /etc/locale.confなので書き換え
    - name: change locale
      lineinfile: >-
        dest=/etc/locale.conf
        state=present
        regexp=^LANG=
        line='LANG="ja_JP.UTF-8"'
# remiのversionは7に書き換え
#    - name: install remi repository
#      command: rpm -Uvh --force http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
    - name: install remi repository
      command: rpm -Uvh --force http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    - name: install man
      yum: name=man state=latest
    - name: install apache
      yum: name=httpd state=latest
    - name: start apache and enabled
      service: name=httpd state=started enabled=yes
    - name: change owner
      file: dest=/var/www/html owner=vagrant recurse=yes
    - name: copy httpd.conf
      copy: src=httpd.conf.custom dest=/etc/httpd/conf/httpd.conf backup=yes
      notify:
        - restart apache
    - name: install gd-last
      yum: name=gd-last enablerepo=remi state=latest
    - name: install php
      yum: name={{item}} enablerepo=remi-php56 state=present
      with_items:
        - php
        - php-common
        - php-pdo
        - php-cli
        - php-devel
        - php-mysqlnd
        - php-mbstring
        - php-gd
        - php-intl
        - php-xml
      notify:
        - restart apache
    - name: copy php.ini.custom
      copy: src=php.ini.custom dest=/etc/php.ini backup=yes
      notify:
        - restart apache

    - name: install mysql repository
      command: rpm -Uvh --force http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
    - name: install mysql
      yum: name={{item}} state=present
      with_items:
        - mysql
        - mysql-devel
        - mysql-server
        - mysql-utilities
    - name: copy my.cnf.custom
      copy: src=my.cnf.custom dest=/etc/my.cnf backup=yes
    - name: start mysql and enabled
      service: name=mysqld state=started enabled=yes
    - name: install ruby dependencies
      yum: name={{item}} enablerepo=remi,epel state=present
      with_items:
        - gcc
        - openssl
        - openssl-devel
        - rpm-build
        - gcc-c++
        - bzip2
        - bzip2-devel
        - libtool
        - zlib
        - zlib-devel
        - httpd-devel
        - openssl-devel
        - curl-devel
        - ncurses-devel
        - gdbm-devel
        - readline
        - readline-devel
        - sqlite
        - sqlite-devel
        - libyaml-devel
        - libffi-devel
        - bison

    - name: check rbenv installed
      command: test -x /home/vagrant/.rbenv
      register: rbenv_present
      become: no
      failed_when: rbenv_present.rc not in [0, 1]
    - name: git clone rbenv
      git: repo=https://github.com/sstephenson/rbenv.git dest=/home/vagrant/.rbenv
      when: rbenv_present.rc == 1
      become: no
    - name: update bash_profile
      copy: src=bash_profile.custom dest=/home/vagrant/.bash_profile backup=yes
      become: no

    - name: check ruby-build installed
      command: test -x /home/vagrant/.rbenv/plugins/ruby-build
      register: rbuild_present
      become: no
      failed_when: rbuild_present.rc not in [0, 1]
    - name: git clone ruby-build
      git: repo=https://github.com/sstephenson/ruby-build.git dest=/home/vagrant/.rbenv/plugins/ruby-build
      when: rbuild_present.rc == 1
      become: no

    - name: update rbenv
      command: git pull --rebase chdir=/home/vagrant/.rbenv
      become: no
    - name: update ruby-build
      command: git pull --rebase chdir=/home/vagrant/.rbenv/plugins/ruby-build
      become: no

    - name: check ruby installed
      shell: /bin/bash -lc "rbenv versions | grep {{ruby_version}}"
      register: ruby_installed
      become: no
      failed_when: ruby_installed.rc not in [0, 1]
    - name: install ruby
      shell: /bin/bash -lc "rbenv install {{ruby_version}} && rbenv rehash && rbenv global {{ruby_version}}"
      when: ruby_installed.rc == 1
      become: no

    - name: check pyenv installed
      command: test -x /home/vagrant/.pyenv
      register: pyenv_present
      become: no
      failed_when: pyenv_present.rc not in [0, 1]
    - name: git clone pyenv
      git: repo=https://github.com/yyuu/pyenv.git dest=/home/vagrant/.pyenv
      when: pyenv_present.rc == 1
      become: no

    - name: check pyvirtual installed
      command: test -x /home/vagrant/.pyenv/plugins/pyenv-virtualenv
      register: pyvirtual_present
      become: no
      failed_when: pyvirtual_present.rc not in [0, 1]
    - name: git clone pyenv-virtual
      git: repo=https://github.com/yyuu/pyenv-virtualenv.git dest=/home/vagrant/.pyenv/plugins/pyenv-virtualenv
      when: pyvirtual_present.rc == 1
      become: no

    - name: update pyenv
      command: git pull --rebase chdir=/home/vagrant/.pyenv
      become: no
    - name: update pyenv-virtualenv
      command: git pull --rebase chdir=/home/vagrant/.pyenv/plugins/pyenv-virtualenv
      become: no

    - name: check python installed
      shell: /bin/bash -lc "pyenv versions | grep {{python_version}}"
      register: python_installed
      become: no
      failed_when: python_installed.rc not in [0, 1]
    - name: install python
      shell: /bin/bash -lc "pyenv install {{python_version}} && pyenv rehash && pyenv global {{python_version}}"
      when: python_installed.rc == 1
      become: no

    - name: check node js installed
      shell: /bin/bash -lc "node -v"
      register: node_installed
      become: no
      failed_when: node_installed.rc not in [0, 1, 127]
    - name: install node script
      shell: /bin/bash -lc "curl -sL https://rpm.nodesource.com/setup | bash -"
      when: node_installed.rc == 127
    - name: install nodejs
      yum: name=nodejs state=latest
      when: node_installed.rc == 127
    - name: install npm
      yum: name=npm state=latest
      when: node_installed.rc == 127

  handlers:
    - name: restart apache
      service: name=httpd state=restarted
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?