はじめに
Ansible
を使って、Certbot
をインストールする機会があったのでメモしておく。
環境
Windows 11 Professional
WSL2 Ubuntu 24.04 LTS
nix (Nix) 2.25.3
Amazon Linux 2023
GitHubリポジトリ
ページ内で記載する
Ansibleの実行環境の構築
AnsiblePlaybookの作成
を含めたGitHubリポジトリは以下に用意した。
https://github.com/katsuobushiFPGA/ansible-apahce-certbot-al2023
Ansibleの実行環境の構築
Ansible
の実行はローカルに構築する。
ローカルには、Nix
が入っているので、nix-shell
を利用してAnsible
を実行する形となる。
shell.nixの作成
適当なディレクトリに、shell.nix
を作成する。
shell.nix
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.ansible
pkgs.openssh
];
}
AnsiblePlaybookの作成
AL2023
にCertbot
をインストールするために、Playbook
を作成する。
ディレクトリ構成についてだが、ベストプラクティスな構成の例は以下のようだ。
ansible-project/
├── ansible.cfg # Ansibleの設定ファイル
├── inventory/ # インベントリファイルを格納
│ ├── production # 本番環境のインベントリ
│ ├── staging # ステージング環境のインベントリ
│ ├── development # 開発環境のインベントリ
│ └── group_vars/ # グループごとの変数
│ ├── all.yml # すべてのグループ共通の変数
│ ├── web.yml # Webサーバー用の変数
│ ├── db.yml # DBサーバー用の変数
│ └── host_vars/ # ホストごとの変数
│ ├── web01.yml # web01サーバー用の変数
│ ├── db01.yml # db01サーバー用の変数
├── playbooks/ # Playbookの格納
│ ├── site.yml # メインのPlaybook(全体を実行)
│ ├── web.yml # Webサーバー用のPlaybook
│ ├── db.yml # DBサーバー用のPlaybook
│ └── roles/ # 役割(Role)のディレクトリ
│ ├── webserver/ # Webサーバーのロール
│ │ ├── tasks/ # 実行タスク
│ │ ├── handlers/ # ハンドラ
│ │ ├── templates/ # テンプレート
│ │ ├── files/ # 配置するファイル
│ │ ├── vars/ # 変数
│ │ ├── defaults/ # デフォルト変数
│ │ ├── meta/ # 依存関係情報
│ │ └── README.md # 説明ファイル
│ ├── database/ # データベースのロール
│ ├── common/ # すべてのサーバーに共通の設定
├── group_vars/ # グループごとの変数(inventory以下でもOK)
├── host_vars/ # ホストごとの変数(inventory以下でもOK)
├── requirements.yml # 依存関係(Galaxyなど)
└── README.md # プロジェクトの説明
ここから少しコンパクトにして作成する。
ディレクトリ構成
├── README.md
├── playbooks
│ ├── hosts
│ ├── hosts.example
│ ├── roles
│ │ ├── apache
│ │ │ └── tasks
│ │ │ └── main.yml
│ │ ├── certbot
│ │ │ └── tasks
│ │ │ └── main.yml
│ │ └── common
│ │ └── tasks
│ │ └── main.yml
│ ├── site.yml
│ └── test.yml
└── shell.nix
EC2インスタンスの作成
※適用するインスタンスが存在する場合はこのセクションは省略してよい。
項目 | 設定値 |
---|---|
名前 | server |
OS | AmazonLinux2023 AMI |
アーキテクチャ | 64ビット(Arm) |
インスタンスタイプ | t4g.micro |
キーペア | server(新規で作成) |
セキュリティグループ | ssh,http,https (自分のIPのみ) |
ストレージ | 8GB (gp3) |
上記の設定で作成する。
SSHでの接続確認
ssh -i [鍵ファイル名] ec2-user@[IPアドレス]
で接続
今回は、鍵ファイル名: server.pem
, IPアドレス: 35.75.20.179
なので、これで接続をする。
ssh -i server.pem ec2-user@35.75.20.179
ログ
ssh -i server.pem ec2-user@35.75.20.179
The authenticity of host '35.75.20.179 (35.75.20.179)' can't be established.
ED25519 key fingerprint is SHA256:9qVxhBh+RaFOpDtvXSmlb8FY4GVYJ+isADjmvmqgOIs.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '35.75.20.179' (ED25519) to the list of known hosts.
, #_
~\_ ####_ Amazon Linux 2023
~~ \_#####\
~~ \###|
~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023
~~ V~' '->
~~~ /
~~._. _/
_/ _/
_/m/'
Ansibleの実行
記事内で作成したAnsible
のプロジェクト(shell.nix
と同階層)に移動をしておく。
hosts.exampleからhostsを作成
cp playbooks/hosts.example playbooks/hosts
vim hosts
hosts
35.75.20.179 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/server.pem
IPアドレスと鍵ファイルの場所を記載する。
Ansible実行環境の構築
shell.nix
がある階層で以下のコマンドを実行する。
nix-shell
[nix-shell:~/workspace/ansible-exec]$
`nix-shell:` が出ていればOK
テスト実行
ansible-playbook -i playbooks/hosts playbooks/test.yml
ログ
[nix-shell:~/workspace/ansible-exec]$ ansible-playbook -i playbooks/hosts playbooks/test.yml
PLAY [Test] ********************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************
[WARNING]: Platform linux on host 35.75.20.179 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python interpreter could
change the meaning of that path. See https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
ok: [35.75.20.179]
TASK [Ping check] **************************************************************************************************************************************************************
ok: [35.75.20.179]
PLAY RECAP *********************************************************************************************************************************************************************
35.75.20.179 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
実行
ansible-playbook -i playbooks/hosts playbooks/site.yml
ログ
[nix-shell:~/workspace/ansible-exec]$ ansible-playbook -i playbooks/hosts playbooks/site.yml
PLAY [Provision] ***************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************
[WARNING]: Platform linux on host 35.75.20.179 is using the discovered Python interpreter at /usr/bin/python3.9, but future installation of another Python interpreter could
change the meaning of that path. See https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
ok: [35.75.20.179]
TASK [common : Set timezone to Asia/Tokyo] *************************************************************************************************************************************
changed: [35.75.20.179]
TASK [common : Confirm system locale] ******************************************************************************************************************************************
ok: [35.75.20.179]
TASK [common : Set system locale to ja_JP.utf8] ********************************************************************************************************************************
changed: [35.75.20.179]
TASK [common : set LANG=ja_JP.UTF-8 in /etc/sysconfig/i18n] ********************************************************************************************************************
changed: [35.75.20.179]
TASK [common : Install packages] ***********************************************************************************************************************************************
changed: [35.75.20.179]
TASK [common : Enable and start rsyslog] ***************************************************************************************************************************************
changed: [35.75.20.179]
TASK [common : Upgrade all packages] *******************************************************************************************************************************************
ok: [35.75.20.179]
TASK [apache : Install Apache] *************************************************************************************************************************************************
changed: [35.75.20.179]
TASK [apache : Install mod_ssl] ************************************************************************************************************************************************
changed: [35.75.20.179]
TASK [apache : Start and enable httpd] *****************************************************************************************************************************************
changed: [35.75.20.179]
TASK [certbot : Install Certbot] ***********************************************************************************************************************************************
changed: [35.75.20.179]
TASK [certbot : daemon-reload] *************************************************************************************************************************************************
ok: [35.75.20.179]
TASK [certbot : Start and enable certbot-renew.timer] **************************************************************************************************************************
changed: [35.75.20.179]
PLAY RECAP *********************************************************************************************************************************************************************
35.75.20.179 : ok=14 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
確認
Apache
アクセス出来ているのでOK
Certbot
サーバにSSHでログインをして以下を確認。
[ec2-user@ip-172-31-40-240 ~]$ certbot --version
certbot 2.6.0
参考
- Certbot を使用して、Amazon Linux 2023 (AL2023) を実行している EC2 インスタンスで Apache または Nginx で HTTPS を有効にする方法を教えてください。
https://repost.aws/ja/articles/AR_doGU0cxQymwf5A1Gl97yA/how-to-use-certbot-to-enable-https-with-apache-or-nginx-on-ec2-instances-running-amazon-linux-2023-al2023
おわりに
次回はこのインスタンスを使って Certbot
で証明書を発行することを実施する。