タイトルの通りですが、ansibleでrailsの環境をvagrant上に作った時の設定を記します。
データベース(MySQL等)の設定は省いています。
バージョン情報は以下になります。
ruby : 2.2.3
Rails : 5.0.1
passenger : 5.1.2
vagrantのディレクトリ下にhostsファイルを作成してpingが通るようにしてください。
また、あらかじめVagrantfile上で以下の2行のコメントアウトは外してください。
config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "private_network", ip: "192.168.33.10"
playbook.ymlのソースコードは以下になります。
- hosts: vagrant
sudo: yes
tasks:
- name: Install git
yum: name=git state=latest
- name: Install rbenv
git: repo=https://github.com/sstephenson/rbenv.git dest=~/.rbenv
- name: Install rbenv-build
git: repo=https://github.com/sstephenson/ruby-build.git dest=~/.rbenv/plugins/ruby-build
- name: ~/.rbenv/ to ~/bash_profile
lineinfile: >
dest="~/.bash_profile"
line="export PATH=$HOME/.rbenv/bin:$PATH"
- name: rbenv init to ~/bash_profile
lineinfile: >
dest="~/.bash_profile"
line='eval "$(rbenv init -)"'
- name: source ~/bash_profile
shell: source ~/.bashrc executable=/bin/bash
- name: Install ruby
command: /bin/bash -lc "rbenv install 2.2.3 && rbenv rehash && rbenv global 2.2.3"
- name: Update gem
command: gem update --system
- name: Install rails
command: /bin/bash -lc "gem install rails --no-ri --no-rdoc --version '=5.0.1'"
- name: rehash rbenv
shell: /bin/bash -lc "rbenv rehash"
- name: Install httpd
yum: name={{ item }} state=latest
with_items:
- httpd
- httpd-devel
- apr-devel
- apr-util-devel
- libcurl-devel
- name: Install passenger
command: /bin/bash -lc "gem install passenger -q --no-rdoc --no-ri -v '5.1.2'"
- name: rehash rbenv
shell: /bin/bash -lc "rbenv rehash"
- name: Install passenger-module
shell: /bin/bash -lc "passenger-install-apache2-module --auto --languages=ruby"
args:
creates: /etc/httpd/conf.d/passenger.conf
- name: Create conf
file: path=/etc/httpd/conf.d/rails.conf state=touch mode="u+rw,g-wx,o-rwx"
- name: Copy conf
copy: >
src=rails.conf
dest=/etc/httpd/conf.d/rails.conf
- name: Restart apache
action: service name=httpd state=restarted
- name: Stop Iptables
action: service name=iptables state=stopped
- name: Install source code
git: repo=https://github.com/〜.git dest=/var/www/html/railsApp #ご自身が所有しているGitのクローンパスを指定してください。
- name: Chmod root
file:
path: /root
mode: 0751
recurse: yes
- name: Chmod root/.rben
file:
path: /root/.rbenv
mode: 0755
recurse: yes
- name: gem bundler install
shell: /bin/bash -lc "gem install bundler"
- name: Install Bundle
shell: /bin/bash -lc "/root/.rbenv/shims/bundle install --without test development" chdir=/var/www/html/railsAppまた、rails.confファイルは予めVagrantのディレクトリ下に配置しておく必要があり、
コードは以下になります。
<VirtualHost *:80> RailsEnv production PassengerEnabled on ServerName sample.jp #ここはそれぞれのドメインに合わせてください。IPでつなぐなら何でもいいです。 DocumentRoot /var/www/html/railsApp/public/ RailsBaseURI / <Directory /var/www/html/railsApp/public/> AllowOverride all Options -MultiViews </Directory> </VirtualHost> ||< >|xml| LoadModule passenger_module /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.1.2/buildout/apache2/mod_passenger.so <IfModule mod_passenger.c> PassengerRoot /root/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.1.2 PassengerDefaultRuby /root/.rbenv/versions/2.2.3/bin/ruby </IfModule> ||< あとは実行コマンドで実行すれば完了です。 >|yml| ansible-playbook -i hosts playbook.yml
処理が終わったら、「192.168.33.10/コントローラパス」にアクセスしてみてください。