Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 94 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,92 +1,125 @@
# Ansible Hands-On Materials
## Ansible Hands-On 講義資料

## Overview
### はじめに

IIJ Bootcamp のAnsibleの講義用教材です。
IIJ Bootcamp における Ansibleの項ではこちらで示す環境を前提に演習を行うため、参加者の方々は以下の作業を実施し、必要な環境を揃えてください。
本リポジトリは、IIJ Bootcamp Ansible ハンズオンの演習用教材です。
このハンズオンでは、Ansible を使ったサーバー設定の自動化を体験します。
最終的には、Webアプリケーションが動作する環境をゼロからコードで構築することを目指します。

## Requirements
講義は、本資料で示す環境が準備できていることを前提に進めます。
参加者の方は、以下の手順に従って、事前にハンズオン環境のセットアップをお願いします。

このハンズオンを実施する上では、以下のセットアップが前提となっています
### 構築する環境

![構成図](./docs/images/network.drawio.png)

### 必要環境

ハンズオンを開始する前に、PC若しくは演習用の環境に以下のソフトウェアがインストールされていることを確認してください。

- git
- Docker
- Docker Compose

## Caution
### 演習における推奨開発環境

本ハンズオンでは、開発環境として **Visual Studio Code (VSCode)** と **Dev Containers** 拡張機能の利用を強く推奨します。

- **Visual Studio Code**: 公式サイトからダウンロードできます。

### 注意事項

このハンズオン環境は演習目的のため、本来は推奨されない設定が含まれています。

- Ansibleによる接続がrootユーザ前提となっている
- root ユーザによる SSH ログインを許可 している
- root パスワードが 簡易な文字列 になっている

これらは本番環境で利用するには不適格な為、
本番環境で利用する際には必ず適切なセキュリティ設定を行ってください。

### セットアップ手順

以下の手順を実行すると、図示されたコンテナ環境が構築されます。


#### Hands-On Material のダウンロード

```bash
git clone https://github.com/iij/ansible-exercise.git
```

このハンズオンの実行では、以下のような問題点があります。
演習の為に敢えて設定していますが、本来は好ましい設定ではないため、本番環境では適切な設定を行ってください
#### コンテナ環境のセットアップ

- rootユーザによるsshログインを許可している
- rootパスワードが安直な文字列になっている
```bash
cd ansible-exercise
docker compose up -d
```

## TO DO
#### コンテナの動作・正常性確認

それでは演習に必要な環境のセットアップを行います。
以下の一連の作業を実施すると図に示したような環境が構築されます。
コンソールコンテナにログインします。

1. Hands-On Materialのダウンロード
```sh
git clone https://github.com/iij/ansible-exercise.git
```
```bash
docker exec -it iijbootcamp_ansible_console bash
```

2. コンテナインフラ環境のセットアップ
対象コンテナに対して疎通確認を行います。

ディレクトリの移動
```bash
cd ansible-exercise
```
```bash
ping <対象のコンテナ>
```

コンテナのセットアップ
```bash
docker compose up -d
```
- 例: host00へのping

3. コンソール端末における最低限のパッケージインストール
<details><summary>実行結果の例</summary>

コンテナログイン
```bash
docker exec -it iijbootcamp_ansible_console bash
```
```bash
ping iijbootcamp_ansible_host00 -c 3
```
```bash

4. 動作確認
```bash
docker exec -it iijbootcamp_ansible_console bash
```
```bash
ping <対象のコンテナ>
```
```bash
ssh <対象のコンテナ>
```
- ssh の鍵登録でyes/noを聞かれるがyesでよい
- ログインパスワードは"ansible"
PING iijbootcamp_ansible_host00 (192.0.2.100) 56(84) bytes of data.
64 bytes from iijbootcamp_ansible_host00.ansible-exercise_vm_net (192.0.2.100): icmp_seq=1 ttl=64 time=0.115 ms
64 bytes from iijbootcamp_ansible_host00.ansible-exercise_vm_net (192.0.2.100): icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from iijbootcamp_ansible_host00.ansible-exercise_vm_net (192.0.2.100): icmp_seq=3 ttl=64 time=0.082 ms
```

</details>

- <対象のコンテナ>は以下の通り
- iijbootcamp_ansible_host00
- iijbootcamp_ansible_host01
- iijbootcamp_ansible_web00
- iijbootcamp_ansible_app00
#### 各コンテナへのSSH接続確認

## Recommended
次に、SSHでログインできることを確認します。

このハンズオンでの推奨環境として`Visual Studio Code`(以下、vscode)を指定します。
ソースコードエディタや開発環境に特にこだわりがない人は、以下の環境を整えておくとスムーズにハンズオンを進めることができます。
- iijbootcamp_ansible_host00
- iijbootcamp_ansible_host01
- iijbootcamp_ansible_web00
- iijbootcamp_ansible_app00

### vscode
- **コマンド**: `ssh <対象のコンテナ名>`
- **パスワード**: `ansible`

vscodeとはマイクロソフトが開発したオープンソースのソースコードエディタです。
拡張機能(extension)をインストールすることで様々な言語のソースコードを効率よく編集することができます。
Stack Overflow 2019 Developer Surveyでは、vscodeが最も人気のある開発者環境ツールとしてランクインしています。
初回接続時には、接続先のホストのフィンガープリントを信頼するか尋ねられます。`yes` と入力して Enter キーを押してください。

[公式サイト](https://code.visualstudio.com/)から環境に合わせてインストールしましょう。
<details><summary>実行例: host00へのSSH接続</summary>

#### Ansible Extension
```bash
[root@ansible_console /ansible]# ssh iijbootcamp_ansible_host00
The authenticity of host 'iijbootcamp_ansible_host00 (192.0.2.100)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'iijbootcamp_ansible_host00' (ED25519) to the list of known hosts.
root@iijbootcamp_ansible_host00's password:
[root@ansible_host00 ~]#
```
`[root@ansible_host00 ~]#` のように、プロンプトが対象コンテナのものに変わればログイン成功です。
`exit` コマンドで `console` コンテナに戻れます。
</details>

vscodeにはRed Hat社よりAnsibleのplaybookを書く為に公式のExtensionが提供されています。
補完や構文チェックなどの機能が備わっているため、可能な限り使うようにしましょう
### トラブルシューティング (よくある質問)

[公式サイト](https://marketplace.visualstudio.com/items?itemName=redhat.ansible)
#### Q. Dockerイメージが取得できない
**A.** 社内ネットワークなど、プロキシ経由でのインターネット接続が必要な環境では、Dockerのプロキシ設定が必要な場合があります。お使いの環境に合わせて、Docker Desktopの「Settings」>「Resources」>「Proxies」から設定を行ってください。

#### Q. コンテナ間でSSH接続ができない
**A.** お使いのPCのセキュリティ機能(SELinux, AppArmor, ファイアウォールなど)がコンテナ間の通信をブロックしている可能性があります。一時的にこれらの機能を無効にして試すか、適切な許可ルールを追加してください。
6 changes: 3 additions & 3 deletions ansible/inventories/hosts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

[web]
web00
ap00
# [exercise]
# host00
# host01
6 changes: 3 additions & 3 deletions ansible/playbooks.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- hosts: exercise

- name: "IIJ Bootcamp playbook"
hosts: exercise
tasks:
- ping:
- ping:
10 changes: 2 additions & 8 deletions ansible/roles/mariadb/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@
password: "{{ db_password }}"
host: localhost

- name: remove remote root
mysql_user:
check_implicit_admin: true
login_user: root
login_password: "{{ db_password }}"
user: root
# host: "{{ ansible_fqdn }}"
state: absent
# - name: remove remote root
# # This task is commented out because it could unintentionally remove root@localhost.

- name: import initialize tasks
import_tasks: init.yml
Expand Down
12 changes: 8 additions & 4 deletions ansible/roles/mysql/tasks/init.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
---
- name: install MySQL-python
yum:
name: MySQL-python
- name: install python3-PyMySQL
ansible.builtin.package:
name: python3-PyMySQL
environment: "{{ proxy_env | default({}) }}"

- block:
- name: create database
mysql_db:
login_user: root
login_password: "{{ db_password }}"
name: "{{ db_name }}"

- name: removes anonymous user account
mysql_user:
login_user: root
login_password: "{{ db_password }}"
name: ''
host_all: yes
state: absent

- name: create database user account
mysql_user:
login_user: root
login_password: "{{ db_password }}"
name: "{{ db_user }}"
password: "{{ db_password }}"
host: "192.0.2.%"
priv: "{{ db_name }}.*:ALL"
state: present

ignore_errors: "{{ ansible_check_mode }}"
ignore_errors: "{{ ansible_check_mode }}"
12 changes: 10 additions & 2 deletions ansible/roles/mysql/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@
notify: restart mysql

- name: start mysql
systemd:
name: mysql
service:
name: mysqld
state: started
enabled: yes

- name: set root password
mysql_user:
login_user: root
check_implicit_admin: true
user: root
password: "{{ db_password }}"
host: localhost

- name: import initialize tasks
import_tasks: init.yml
tags: init
2 changes: 1 addition & 1 deletion ansible/roles/webapp/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
name: app
state: started
enabled: yes
daemon_reload: yes
daemon_reload: yes
Binary file added docs/images/network.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.