diff --git a/README.md b/README.md index ce5b69b..326c936 100644 --- a/README.md +++ b/README.md @@ -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. コンソール端末における最低限のパッケージインストール +
実行結果の例 - コンテナログイン - ```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 +``` +
-- <対象のコンテナ>は以下の通り - - 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/)から環境に合わせてインストールしましょう。 +
実行例: host00へのSSH接続 -#### 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` コンテナに戻れます。 +
-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, ファイアウォールなど)がコンテナ間の通信をブロックしている可能性があります。一時的にこれらの機能を無効にして試すか、適切な許可ルールを追加してください。 diff --git a/ansible/inventories/hosts b/ansible/inventories/hosts index 8408135..b6c18df 100644 --- a/ansible/inventories/hosts +++ b/ansible/inventories/hosts @@ -1,4 +1,4 @@ -[web] -web00 -ap00 +# [exercise] +# host00 +# host01 diff --git a/ansible/playbooks.yml b/ansible/playbooks.yml index 130d7d4..381313d 100644 --- a/ansible/playbooks.yml +++ b/ansible/playbooks.yml @@ -1,5 +1,5 @@ --- -- hosts: exercise - +- name: "IIJ Bootcamp playbook" + hosts: exercise tasks: - - ping: + - ping: diff --git a/ansible/roles/mariadb/tasks/main.yml b/ansible/roles/mariadb/tasks/main.yml index a74fe06..ed943f8 100644 --- a/ansible/roles/mariadb/tasks/main.yml +++ b/ansible/roles/mariadb/tasks/main.yml @@ -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 diff --git a/ansible/roles/mysql/tasks/init.yml b/ansible/roles/mysql/tasks/init.yml index 4ac6e5f..24d210e 100644 --- a/ansible/roles/mysql/tasks/init.yml +++ b/ansible/roles/mysql/tasks/init.yml @@ -1,17 +1,20 @@ --- -- 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 @@ -19,10 +22,11 @@ - 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 }}" \ No newline at end of file + ignore_errors: "{{ ansible_check_mode }}" diff --git a/ansible/roles/mysql/tasks/main.yml b/ansible/roles/mysql/tasks/main.yml index d3268be..ca76536 100644 --- a/ansible/roles/mysql/tasks/main.yml +++ b/ansible/roles/mysql/tasks/main.yml @@ -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 \ No newline at end of file diff --git a/ansible/roles/webapp/tasks/main.yml b/ansible/roles/webapp/tasks/main.yml index 1445a5b..ffc63ea 100644 --- a/ansible/roles/webapp/tasks/main.yml +++ b/ansible/roles/webapp/tasks/main.yml @@ -52,4 +52,4 @@ name: app state: started enabled: yes - daemon_reload: yes \ No newline at end of file + daemon_reload: yes diff --git a/docs/images/network.drawio.png b/docs/images/network.drawio.png new file mode 100644 index 0000000..9004319 Binary files /dev/null and b/docs/images/network.drawio.png differ