Rails6の開発環境をAWS上に自動構築する⑤

Rails6の開発環境をAWS上に自動構築する⑤

Rails6の開発環境をAnsibleで自動構築する準備をします。

toshioshimo.hatenablog.com

前回は、テストでapache(httpsサーバー)がインストール、起動できるかテストしました。

続いて、Rails6の環境構築において、前段のライブラリ、DBのインストールまでを行ってみます。。

toshioshimo.hatenablog.com

f:id:TOSHIOSHIMO:20191215222807p:plain

Playbookを作成する

AnsibleはPlaybookと呼ばれるYAML形式のファイルを元に自動構築を行う仕組みです。

ホームディレクトリにrailssetup.ymlという名前で作ってみます。

# sudo vi railssetup.yml

内容は以下のように記述します。インデントの文字数や、ハイフン(-)、コロン(:)の使い方に注意です。

---
- 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

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]

TASK [gcc-c++] *****************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [glibc-headers] ***********************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [openssl-devel] ***********************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [readline] ****************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [libyaml-devel] ***********************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [zlib] ********************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [mariaDB] *****************************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [mariadb-server] **********************************************************
ok: [xxx.xxx.xxx.xxx]

TASK [git] *********************************************************************
ok: [xxx.xxx.xxx.xxx]
PLAY RECAP *********************************************************************
xxx.xxx.xxx.xxx : ok=11 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

幸い現場で経験していたので、すんなり通りました。数年経っても覚えているものですね。

インストールされたことを確認する

自動構築された側にログインし"yum info"コマンドで確認します。

対象となったパッケージを引数として羅列し、エラーが出ずパッケージ情報が常時されればOKです。

# yum info gcc-c++ glibc-headers openssl-devel readline libyaml-devel zlib mariadb mariadb-server git
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
インストール済みパッケージ
名前 : gcc-c++
アーキテクチャー : x86_64
バージョン : 7.3.1
リリース : 9.amzn2
容量 : 41 M
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : C++ support for GCC
URL : http://gcc.gnu.org
ライセンス : GPLv3+ and GPLv3+ with exceptions and GPLv2+ with
: exceptions and LGPLv2+ and BSD
説明 : This package adds C++ support to the GNU Compiler
: Collection. It includes support for most of the current
: C++ specification, including templates and exception
: handling.

名前 : git
アーキテクチャー : x86_64
バージョン : 2.23.3
リリース : 1.amzn2.0.1
容量 : 377 k
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : Fast Version Control System
URL : https://git-scm.com/
ライセンス : GPLv2
説明 : Git is a fast, scalable, distributed revision control
: system with an unusually rich command set that provides
: both high-level operations and full access to internals.
:
: The git rpm installs common set of tools which are usually
: using with small amount of dependencies. To install all
: git packages, including tools for integrating with other
: SCMs, install the git-all meta-package.

名前 : glibc-headers
アーキテクチャー : x86_64
バージョン : 2.26
リリース : 35.amzn2
容量 : 2.2 M
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : Header files for development using standard C libraries.
URL : http://www.gnu.org/software/glibc/
ライセンス : LGPLv2+ and LGPLv2+ with exceptions and GPLv2+
説明 : The glibc-headers package contains the header files
: necessary for developing programs which use the standard C
: libraries (which are used by nearly all programs). If you
: are developing programs which will use the standard C
: libraries, your system needs to have these standard header
: files available in order to create the executables.
:
: Install glibc-headers if you are going to develop programs
: which will use the standard C libraries.

名前 : libyaml-devel
アーキテクチャー : x86_64
バージョン : 0.1.4
リリース : 11.amzn2.0.2
容量 : 912 k
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : Development files for LibYAML applications
URL : http://pyyaml.org/
ライセンス : MIT
説明 : The libyaml-devel package contains libraries and header
: files for developing applications that use LibYAML.

名前 : mariadb
アーキテクチャー : x86_64
エポック : 1
バージョン : 5.5.64
リリース : 1.amzn2
容量 : 49 M
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : A community developed branch of MySQL
URL : http://mariadb.org
ライセンス : GPLv2 with exceptions and LGPLv2 and BSD
説明 : MariaDB is a community developed branch of MySQL.
: MariaDB is a multi-user, multi-threaded SQL database
: server. It is a client/server implementation consisting of
: a server daemon (mysqld) and many different client
: programs and libraries. The base package contains the
: standard MariaDB/MySQL client programs and generic MySQL

名前 : mariadb-server
アーキテクチャー : x86_64
エポック : 1
バージョン : 5.5.64
リリース : 1.amzn2
容量 : 58 M
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : The MariaDB server and related files
URL : http://mariadb.org
ライセンス : GPLv2 with exceptions and LGPLv2 and BSD
説明 : MariaDB is a multi-user, multi-threaded SQL database
: server. It is a client/server implementation consisting of
: a server daemon (mysqld) and many different client
: programs and libraries. This package contains the MariaDB
: server and some accompanying files and directories.
: MariaDB is a community developed branch of MySQL.

名前 : openssl-devel
アーキテクチャー : x86_64
エポック : 1
バージョン : 1.0.2k
リリース : 19.amzn2.0.3
容量 : 3.1 M
リポジトリー : installed
提供元リポジトリー : amzn2-core
要約 : Files for development of applications which will use
: OpenSSL
URL : http://www.openssl.org/
ライセンス : OpenSSL
説明 : OpenSSL is a toolkit for supporting cryptography. The
: openssl-devel package contains include files needed to
: develop applications which support various cryptographic
: algorithms and protocols.

名前 : readline
アーキテクチャー : x86_64
バージョン : 6.2
リリース : 10.amzn2.0.2
容量 : 450 k
リポジトリー : installed
要約 : A library for editing typed command lines
URL : http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html
ライセンス : GPLv3+
説明 : The Readline library provides a set of functions that
: allow users to edit command lines. Both Emacs and vi
: editing modes are available. The Readline library includes
: additional functions for maintaining a list of
: previously-entered command lines for recalling or editing
: those lines, and for performing csh-like history expansion
: on previous commands.

名前 : zlib
アーキテクチャー : x86_64
バージョン : 1.2.7
リリース : 18.amzn2
容量 : 177 k
リポジトリー : installed
要約 : The compression and decompression library
URL : http://www.zlib.net/
ライセンス : zlib and Boost
説明 : Zlib is a general-purpose, patent-free, lossless data
: compression library which is used by many different
: programs.

利用可能なパッケージ
名前 : readline
アーキテクチャー : i686
バージョン : 6.2
リリース : 10.amzn2.0.2
容量 : 199 k
リポジトリー : amzn2-core/2/x86_64
要約 : A library for editing typed command lines
URL : http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html
ライセンス : GPLv3+
説明 : The Readline library provides a set of functions that
: allow users to edit command lines. Both Emacs and vi
: editing modes are available. The Readline library includes
: additional functions for maintaining a list of
: previously-entered command lines for recalling or editing
: those lines, and for performing csh-like history expansion
: on previous commands.

名前 : zlib
アーキテクチャー : i686
バージョン : 1.2.7
リリース : 18.amzn2
容量 : 91 k
リポジトリー : amzn2-core/2/x86_64
要約 : The compression and decompression library
URL : http://www.zlib.net/
ライセンス : zlib and Boost
説明 : Zlib is a general-purpose, patent-free, lossless data
: compression library which is used by many different
: programs.

インストールだけなら誰でもできる

インストール作業なら、そのままplaybookに書けば良いというのが分かりました。ただこれではシェルスクリプト言語で行うようなバッチ処理と変わりません。これからがAnsibleの本領発揮です。git cloneや、各種設定ファイル更新、サービスのスタート等入るので今回より煩雑になります。

初めてのAnsible

初めてのAnsible