開発を忘れたインフラエンジニアがRailsを学んだ記録

開発を忘れたインフラエンジニアがRailsを学んだ記録

今から20年前、電話交換機(固定電話/3G FOMA)の制御系プログラマーとしてキャリアをスタートさせました。その後、商社系SIerにて上流工程やセールス寄りの仕事に移行したので、プログラム開発は全く得意ではないエンジニアです。

そんな私が、このたびRailsアプリの開発を学んだ記録です。

f:id:TOSHIOSHIMO:20201021234635j:plain

スキルセット

星マークで表すとこんな感じです。インフラのディープな部分と、セールス寄りが得意であり、フロントエンド開発はほぼ出来ません。世間一般、大多数のエンジニア像(開発プログラマー)とはかなり違うような気がします。

・プログラミング  ★

・インフラ/サーバー ★★★★★

・フロントエンド  ★

・セールス     ★★★

・マネジメント   ★★★★

・コンサル     ★★★★★

・デザイン     ★★★

1日目(知識ゼロ)〜3日目

・ローカルPC(Mac)でRails6のサーバーを最小限の構成で動作させた

・使用DBを変更(Puma→mariaDB)してRails6を起動させた

・scaffoldを使ってタスク管理アプリの動作を確認した

こちらの本はRails5用ですが、6との差分を確認しつつガイドとして使いました。

 MySQLの本が5系で古くなっていたので買い替えました。

 1週間後

AWSでRails6 + mariaDBの環境を構築し手順として確立した

toshioshimo.hatenablog.com

・scaffoldを使ってタスク管理アプリが動作することを確認した

・BootStrapのテンプレートを適用、デザインをカスタマイズした

・ユーザー管理画面を作成した

・ユーザーの作成、編集、削除が出来るようにした

・ユーザー管理テーブルに管理者フラグを追加した

・管理者/一般ユーザーの権限で操作できる内容が変わるようにした

 2週間後

・ログイン画面の作成、ログイン・ログアウト機能を追加した

・ログインパスワードをハッシュ化した

・ログイン状況を判断して各ページの読み込みに制限をかけた

flash機能でメッセージが出るようにした

・アラート通知に使うテーブルを作成した

・アラートの作成、編集、削除が出来るようにした

・ログイン画面、ユーザー/アラート登録時のvalidation機能を追加した

 3週間後

 追加で本を購入

Ruby on Rails 6 実践ガイド impress top gearシリーズ
 

REST API追加、POSTツールからのデータ受信、DB蓄積を確認した

・IoTデバイスよりPOSTされたデータ受信、DB蓄積を確認した

・受信データよりアラートテーブルをサーチ、Amazon SES経由のメール送信を確認した

・Chart.jsで直近の蓄積データがグラフ表示されることを確認した

・蓄積データのCSV出力機能を追加した

とりあえず動くものは出来てしまう

17年越しに開発にチャレンジしましたが、プログラミングというよりは、何だかテンプレートを触っている感覚です。Railsは便利なメソッド等がバッチリ揃っていて、SQLなど覚えなくともアプリが作れてしまいます。これならプログラミングスクールに短期で通って、「プログラマー」と名乗り即戦力として働けるのも納得です。

しかし本当にそれで良いの?

ただ、C言語アセンブラ機械語を学んでいた身からすると、それで本当にデバッグができるのか、かなり疑問です。ミドルウェアやOSに近いカーネルの部分が関わると途端に何もできなくなり、バグも多くなる気がします。Railsのログがどうやって出力されているのかも知らないのでは?(ログに残らない部分のデバッグができない)Ruby on Railsから入ったエンジニアにアプリ開発を任せるのは、ちょっと不安感あります。

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

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

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

toshioshimo.hatenablog.com

 

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

toshioshimo.hatenablog.com

f:id:TOSHIOSHIMO:20191215222807p:plain

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サーバーの開始を行います。

 

Ansible実践ガイド

Ansible実践ガイド

 

 

 

 

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

 

 

 

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

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

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

 

toshioshimo.hatenablog.com

前回は、自動構築対象のEC2にコマンドが通るかテストしました。

続いて、対象のEC2に何らかのサーバープログラムをインストールして、起動させることをしてみましょう。いきなり完璧を目指すと何が原因で不具合が起きているのか分からなくなるので、少しづつ進める・確認する・NGなら元に戻してみる の繰り返しが重要です。

toshioshimo.hatenablog.com

f:id:TOSHIOSHIMO:20191215222807p:plain

Playbookを作成する

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

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

# sudo vi test.yml

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

---
- hosts: all
become: yes
tasks:
- set_fact:
ansible_facts:
pkg_mgr: yum
- name: apache install
yum: name=httpd state=latest
- name: apache start
service: name=httpd state=started

内容をざっくり説明すると 

・/etc/hosts/ansibleにあるhostsファイルに記載のある全てのホストを対象にする

・sudo権限を有効にする(become)

・パッケージマネージャーとしてyumを指定する(ansible v1.8からdnfが優先になっているのでyum使用を明示する)

・httpサーバーのapacheの最新版をインストールし、サービスをスタートさせる

Ansibleを実行してみる

ansibleコマンドを実行してみます。

# ansible-playbook ./test.yml --private-key=./.ssh/(SSH接続のprivatekey).pem

結果

PLAY [all] **************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
[WARNING]: Platform linux on host xxx.xxx.xxx.xxx 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 [apache install] ***************************************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

TASK [apache start] *****************************************************************************************************************************
changed: [xxx.xxx.xxx.xxx]

PLAY RECAP **************************************************************************************************************************************
xxx.xxx.xxx.xxx : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

サービスのインストールと起動を確認する

自動構築された側にログインし、apacheのプロセスが動作しているか確認します。

# ps -ef | grep httpd
root 3334 1 0 11:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3335 3334 0 11:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3336 3334 0 11:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3337 3334 0 11:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3338 3334 0 11:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3339 3334 0 11:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
ec2-user 3399 2597 0 11:16 pts/0 00:00:00 grep --color=auto httpd 

無事httpd(Webサーバー)が起動しています。パブリックIPまたはDNS名でアクセスしてみます。

f:id:TOSHIOSHIMO:20201015204448p:plain

無事apacheのスタートページが起動しました。

次回はRails6を自動構築します。

Ansibleは色々変わっていた

3〜4年前にAmazon Linux2は存在しませんでしたし、Ansibleのバージョンも上がってだいぶ作りが変わっています。記憶を辿りながら実行しましたが、つまずきも多かったです。

 

 

複業と情報発信の話

複業と情報発信の話

今回は、複業と情報発信の話をしたいと思います。この辺りはあらぬ誤解を防ぐためここで表明しておきたいと思います。

何が言いたいのか

ここで言いたいのは、「SNSの投稿回数、投稿内容でその人の行動全てが把握できる訳はない」ということです。SNSへの投稿は本人が事実を都合よく切り取り、感想や思いを加えて表現することができます。発信したくないことは伏せることができます。内容が正しいか、第三者のチェック機関もありません。

SNSと妄想

以上を踏まえて、SNSの投稿を見て頭の中に浮かぶ発信者本人は、半分「虚像」みたいなものだと思います。投稿の言い回しや表現、受け手側の状況で、どのようにも解釈できます。

例えばこういう妄想はどうでしょうか。

・投稿回数が多い→SNSばかりしている

・複業の内容が多い→本業に集中していない

・遊んでいる内容が多い→勉強していない

全部関連性が無いです。関連性を確かめるときには逆でも成り立つかを確認すれば良いです。

SNSばかりしている→投稿回数が多い

・本業に集中していない→複業の内容が多い

・勉強していない→遊んでいる内容が多い

やはり関連性が無いです。本当にそうなのか、実態を確認していませんし、ただの妄想に過ぎません。

「暇な人」説

そもそもなぜそこまで他人の投稿に興味が持てるのか全然分かりません。自分のことに集中していれば、他人のことは気にならないはずです。SNSで様子を伺っても情報の精度が荒すぎて何もあてにならないので、直接話し合いをした方が良いのではと思います。そもそもSNSってそういう「様子を伺うツール」「監視するツール」ですか?

暇な人の暇つぶしにも見えます。コントロール欲が強い人は、自分の思い通りにならず良くは思わないでしょうね(浅い妄想です)仕事での関係なら、業務で契約している時間以外の使い方は個人の自由であり、拘束する権限はありません。プライベートに過度に干渉するのは、よく思われないですよね。

そういう場からは離れる

勝手な妄想を当の本人に指摘し矯正する、当の本人はそれに反論する。この時間は不毛で無意味じゃないでしょうか。そういう場からはいち早く離れましょう。

 

 

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

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

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

お試しで、自動構築対象のEC2にコマンドが通るかテストしてみます。

toshioshimo.hatenablog.com

f:id:TOSHIOSHIMO:20191215222807p:plain

Ansibleで操作する対象のホストを設定する

Ansibleは、/etc/ansible/hostsに記述されたホストを参照してくれます。

# cd /etc
# mkdir ansible
# cd ansible
# sudo vi hosts

hostsファイルに対象のEC2インスタンスIPアドレスまたはホスト名記述します。

ホスト名を記述する際は、名前解決できるようにしておきましょう。

[Web]
xxx.xxx.xxx.xxx (自動構築対象のローカルIP)

 ここで、[web]を記載がありますが、無くても動作します。

これは「グループ」という概念で、Ansible実行時に対象のIPを分けることができます。

"all"と指定した場合は、全て実行されます。

Ansibleを実行してみる

ホームディレクトリに戻り、ansibleコマンドを実行してみます。

# ansible all -m command -a "date" --private-key=(SSHキーの場所)
[WARNING]: Platform linux on host xxx.xxx.xxx.xxx 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.
xxx.xxx.xxx.xxx | CHANGED | rc=0 >>
2020年 10月 10日 土曜日 00:47:28 UTC

 "date"コマンドの結果が返ってくれば成功です。自動構築対象のEC2でdateコマンドを実行し、その結果が返ってきたということです。

ansible all〜としていますが、このallは先程の/etc/ansible/hostsに記載されているホスト全て、グループ関係無く実行するという意味です。

WARNINGの行は、Ansible2.8から実装された機能で、ansibleが使用するPythonのインタラプタを検出する仕組みから出ているようです。

試しにAnsibleのバージョンを確認してみます。

# ansible --version
ansible 2.10.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.18 (default, Aug 27 2020, 21:22:52) [GCC 7.3.1 20180712 (Red Hat 7.3.1-9)]

使用するpythonに2.7.18が指定されています。

インストールされているpythonのバージョンを確認してみます、

# ls -la python*
lrwxrwxrwx 1 root root 7 9月 21 21:11 python -> python2
lrwxrwxrwx 1 root root 14 9月 21 21:11 python-config -> python2-config
lrwxrwxrwx 1 root root 9 9月 21 21:11 python2 -> python2.7
lrwxrwxrwx 1 root root 16 9月 21 21:11 python2-config -> python2.7-config
-rwxr-xr-x 1 root root 7048 8月 27 21:23 python2.7
-rwxr-xr-x 1 root root 1846 8月 27 21:23 python2.7-config

Amazon Linux2のデフォルトでは、Python3系が入っていないようです。

このWARNINGを非表示とするにはansible.cfgファイルでインタラプタを設定する、インベントリファイルで設定するなどやり方はあるのですが、今回のテストの本筋から逸れますし、動作に支障が無いので無視しておきます。

今回のまとめ

Ansibleの実行に最低限の設定をして、構築対象(下図左のインスタンス)にリモートでコマンドを実行されることを確認しました。AnsibleではPlaybookというファイルに沿って、順番にコマンドを実行していくことで、自動構築を実現しています。

f:id:TOSHIOSHIMO:20201010104455p:plain

 

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

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

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

toshioshimo.hatenablog.com

f:id:TOSHIOSHIMO:20191215222807p:plain

Ansibleを動作させる用のEC2インスタンスを作成する

大きな負荷がかかるものではないので、最小限のスペックでOKです。

・東京リージョン(ap-northeast1)、OSはAmazon Linux2

・t2.microまたはt3.micro(無料枠が残っていればt2、それ以降はt3の方が安い)

・ストレージ (EBS)は8GB SSDを割り当て

インスタンスに割り当てるSecurity Groupの設定で、ポート80番(HTTP)を許可

・自動割当パブリックIPは有効

・インターネット側からアクセスできるようインターネットゲートウェイの設置、VPCの設定

詳細は省略します。過去の記事はこちら。

toshioshimo.hatenablog.com

f:id:TOSHIOSHIMO:20201003105225p:plain

Ansibleをインストールする

Python-devel(Pythonライブラリ)、openssl(ssl接続に必要)、gcc(GNUコンパイラ)が必要です。

# sudo yum -y install python-devel openssl-devel gcc git

パッケージ管理システムのpipをインストールします。easy_installはインスタンス起動直後から使えるみたいですね。

# sudo easy_install pip

Ansibleをインストールします。

# sudo pip install ansible

Ansibleのバージョンを確認してインストール完了を確認します。 

# ansible --version
ansible 2.10.2
config file = None
configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.18 (default, Aug 27 2020, 21:22:52) [GCC 7.3.1 20180712 (Red Hat 7.3.1-9)]

自動構築される側のEC2インスタンスをつくる

f:id:TOSHIOSHIMO:20201005214052p:plain

上図でいう左側、自動構築される側のEC2インスタンスを作成します。

インスタンスタイプなどの設定は、Ansibleサーバーと同じにしておきましょう。

作成したら分かりやすいようNameを付けておきます。

秘密鍵(〜.pem)を忘れずに保管しましょう。

f:id:TOSHIOSHIMO:20201008213811p:plain

Ansibleサーバーに秘密鍵をアップロードする

ローカルPCからscpコマンドで、先程の秘密鍵(〜.pem)をアップロードします。

# scp -i (Ansibleサーバーの秘密鍵).pem (自動構築される側の秘密鍵).pem ec2-user@13.230.164.53:/home/ec2-user/.ssh/
railstest.pem 100% 1704 86.4KB/s 00:00  

Ansibleサーバーから自動構築対象のEC2にSSH接続してみる

問題無く接続できればOKです。AnsibleはSSH接続を通して、自動構築を行います。

# ssh -i "(自動構築される側の秘密鍵).pem" ec2-user@(自動構築される側のプライベートIP)

次回は、構築対象を定義するinventlyファイル、site.ymlファイル、実際にリモートで実行するコマンド、スクリプトを記述するtasksファイルを説明しながら作成していきます。

仮想環境にIaCのスキルは必須 

オンプレミスの物理サーバーなら基本手順に沿って人が構築することになりますが、クラウド等の仮想環境ではコード化して自動構築(Infrastracure as Code)できるようにしておくことが必須になります。2人一組で読み合わせをしながらコマンドを投入していた時代もありますが、今は構築にかかる工数を最小限とし、確実に構築できる「再現性」を上げて効率化する時代です。 

現場で使っていたテキスト

※機密情報は破棄してます。3〜4年前だったと思いますが、たいした書籍も出版されておらず、Amazon電子書籍を購入してプリントアウト、現場で使ってました。某メーカーのクラウドサービスの裏側を作っていました。知識ゼロの状態から、4ヶ月間「漬け」になりましたが、得たものは大きかったです。

f:id:TOSHIOSHIMO:20201008221048j:plain 

入門Ansible

入門Ansible