Ansibleを使ってAmazonLinux 2023にCertbotをインストールする

はじめに

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の作成

AL2023Certbotをインストールするために、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
OSAmazonLinux2023 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

http apache-confirm-01

https apache-confirm-02

アクセス出来ているのでOK

Certbot

サーバにSSHでログインをして以下を確認。

[ec2-user@ip-172-31-40-240 ~]$ certbot --version
certbot 2.6.0

参考

おわりに

次回はこのインスタンスを使って Certbotで証明書を発行することを実施する。

Hugo で構築されています。
テーマ StackJimmy によって設計されています。