Rails6の開発環境をAWS上に自動構築する⑥
Rails6の開発環境をAnsibleで自動構築する準備をします。
toshioshimo.hatenablog.com
前回は、Rails6の環境構築において、前段のライブラリ、DBのインストールまでを行いました。今回はrailsのインストールまでを行ってみます。
toshioshimo.hatenablog.com
Playbookを作成する
AnsibleはPlaybookと呼ばれるYAML形式のファイルを元に自動構築を行う仕組みです。
ホームディレクトリにrailssetup.ymlという名前で作ってみます。
# sudo vi railssetup.yml
内容は以下のように記述します。青字が今回追加した部分です。もう一度playbookを実行することになりますが、重複しても大丈夫なようにできています。
---
- hosts: all
become: yes
tasks:
- set_fact:
ansible_facts:
pkg_mgr: yum
- name: gcc-c++
yum: name=gcc-c++ state=latest
- name: glibc-headers
yum: name=glibc-headers state=latest
- name: openssl-devel
yum: name=openssl-devel state=latest
- name: readline
yum: name=readline state=latest
- name: libyaml-devel
yum: name=libyaml-devel state=latest
- name: zlib
yum: name=zlib state=latest
- name: mariaDB
yum: name=mariadb state=latest
- name: mariadb-server
yum: name=mariadb-server state=latest
- name: git
yum: name=git state=latest
- name: rbenv_git
git:
repo: https://github.com/sstephenson/rbenv.git
dest: /home/ec2-user/.rbenv
- command: echo export PATH="/home/ec2-user/.rbenv/bin:$PATH" >> /home/ec2-user/.bash_profile
- command: echo eval \"$(rbenv init -)\" >> /home/ec2-user/.bash_profile
- shell: source /home/ec2-user/.bash_profile
- file:
path: \"$(rbenv root)\"/plugins
state: directory
- name: rbenv_build_git
git:
repo: https://github.com/rbenv/ruby-build.git
dest: \"$(rbenv root)\"/plugins/ruby-build
- command: rbenv install 2.7.1
- command: rbenv global 2.7.1
- name: rails_install
gem:
name: rails
- command: bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
Ansibleを実行してみる
ansibleコマンドを実行してみます。
# ansible-playbook ./railssetup.yml --private-key=./.ssh/(SSH接続のprivatekey).pem
結果
PLAY [all] **************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************
[WARNING]: Platform linux on host 10.0.4.148 is using the discovered Python
interpreter at /usr/bin/python, but future installation of another Python
interpreter could change the meaning of that path. See https://docs.ansible.com
/ansible/2.10/reference_appendices/interpreter_discovery.html for more
information.
ok: [xxx.xxx.xxx.xxx]
TASK [set_fact] ****************************************************************
ok: [xxx.xxx.xxx.xxx]
〜(省略)〜
PLAY RECAP *******************************************************************************************************************
xxx.xxx.xxx.xxx : ok=19 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
パス関係でつまずく
パスが通らない箇所はひとまず直書きで回避しました。次回は、DBの作成、起動とRailsサーバーの開始を行います。