ステップ2. Vagrantを使って仮想マシンを立ち上げ、ansibleでrailsアプリケーションをビルドする
-
Vagrantfileの作成
まずは、先ほど作成したVagrantfileを以下のように変更してください。Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "forwarded_port", guest: 3000, host: 3000config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
endconfig.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.become = true
end
end
これにより、Ansibleが使用できるようになります。Ansibleのインストール
Ansibleをインストールするために、以下のコマンドを実行してください。sql
sudo apt-get update
sudo apt-get install -y ansible
playbook.ymlの作成
以下のコマンドを実行して、playbook.ymlを作成します。bash
touch playbook.yml
playbook.ymlには、以下のように記述します。
yaml
---
- hosts: all
become: yestasks:
- name: Install required packages
apt: name={{ item }} state=latest
with_items:
- git
- libssl-dev
- libreadline-dev
- zlib1g-dev
- libsqlite3-dev
- nodejs
- postgresql
- libpq-dev- name: Install Ruby using rbenv
git: repo=https://github.com/rbenv/rbenv.git dest=~/.rbenv
register: rbenv_install
become_user: vagrant
environment:
PATH: "{{ ansible_env.PATH }}:~/.rbenv/bin"
changed_when: rbenv_install.changed- name: Update ~/.bashrc for rbenv
lineinfile: path=~/.bashrc line="{{ item }}"
with_items:
- 'export PATH="$HOME/.rbenv/bin:$PATH"'
- 'eval "$(rbenv init -)"'
become_user: vagrant
environment:
PATH: "{{ ansible_env.PATH }}:~/.rbenv/bin"- name: Install Ruby
shell: rbenv install 3.0.1
become_user: vagrant
environment:
PATH: "{{ ansible_env.PATH }}:~/.rbenv/bin"
when: rbenv_install.changed- name: Set global Ruby version
shell: rbenv global 3.0.1
become_user: vagrant
environment:
PATH: "{{ ansible_env.PATH }}:~/.rbenv/bin"- name: Install Bundler
shell: gem install bundler
become_user: vagrant
environment:
PATH: "{{ ansible_env.PATH }}:~/.rbenv/bin"- name: Create database
shell: createdb -U myapp -h localhost myapp_production
become_user: postgres- name: Clone Rails application from repository
git: repo=<repository-url> dest=~/myapp
become_user: vagrant- name: Install Rails application dependencies
shell: bundle install --path vendor/bundle
args:
chdir
Railsアプリケーションのデプロイ
以下の手順で、Railsアプリケーションをデプロイします。playbook.ymlと同じディレクトリに、deploy.ymlという名前のファイルを作成します。
bash
touch deploy.yml
deploy.ymlには、以下のように記述します。
yaml
Copy code
---
- hosts: all
become: yestasks:
- name: Install Bundler
shell: gem install bundler
become_user: vagrant
environment:
PATH: "{{ ansible_env.PATH }}:~/.rbenv/bin"- name: Install Rails application dependencies
shell: bundle install --path vendor/bundle
args:
chdir: ~/myapp- name: Create database tables
shell: bundle exec rails db:migrate RAILS_ENV=production
args:
chdir: ~/myapp- name: Start Rails server
shell: bundle exec rails server -d -b 0.0.0.0 -e production
args:
chdir: ~/myapp
playbook.ymlと同じディレクトリに、vars.ymlという名前のファイルを作成します。bash
touch vars.ymlvars.ymlには、以下のように記述します。
yaml
---
repository_url: https://github.com// .git
playbook.ymlを実行して、Ansibleによるプロビジョニングを実行します。playbook.ymlの実行が終了したら、以下のコマンドを実行して、仮想マシンにSSH接続します。
Copy code
vagrant ssh
仮想マシンのコマンドラインで、以下のコマンドを実行して、Railsアプリケーションをデプロイします。
css
ansible-playbook deploy.yml -e @vars.yml
これにより、GitHubのリポジトリからRailsアプリケーションがダウンロードされ、必要なパッケージがインストールされます。
また、データベースのテーブルが作成され、Railsサーバーが起動されます。