Ansible を使って Ubuntu 22.04 に Docker をインストールして PostgresSQL を起動する
はじめに
今回はAnsibleを使ってDocker のインストールとイメージを作成して
コンテナの起動はサーバーにSSH接続してコマンドを実行する方法となります。
環境
Ubuntu 22.04
鍵認証によるSSH
ssh-leygenコマンドを使ってSSHに使用する秘密鍵と公開鍵を作成します。
今回キーペアの作成場所は初期値 /root/.ssh/ のままで、パスフレーズは設定していません。
root@d4988cd12c43:/# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa Your public key has been saved in /root/.ssh/id_rsa.pub The key fingerprint is: SHA256:OHEWEvbb895QIx+oomZn7zAa8Mxn0FvjTucRxyDjXGk root@d4988cd12c43 The key's randomart image is: +---[RSA 3072]----+ | +.. | | . o . . | | . + o E | | * = = + | | . + S B + * | | = o + = * o | | = B + = . | | o*o* + + | | ooo o+ o . | +----[SHA256]-----+
ssh-copy-idコマンドを使って、先ほど作成した公開鍵をSSH接続するサーバーに保存します。
root@d4988cd12c43:/# ssh-copy-id ubuntu@192.168.11.57 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys ubuntu@192.168.11.57's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'ubuntu@192.168.11.57'" and check to make sure that only the key(s) you wanted were added.
ansible-playbook の実行
init フォルダ内にテーブルを作成するための init.sql を保存しています。
C:. │ inventry.ini │ playbook.yml │ └─postgresql │ Dockerfile │ └─init init.sql
今回なぜか inventry.ini にSSHの接続情報を保存しないと play-book が使えない状況でした。
inventry.ini
target ansible_host=192.168.11.57 ansible_ssh_user=ubuntu aniansible_ssh_pass=ubuntu [target:var] ansible_python_interpreter=/usr/bin/python3
また ansible-playbook コマンドに --ask-pass と --ask-become-pass のオプションが必要でした。
root@d4988cd12c43:/ansible# ansible-playbook -i inventry.ini playbook.yml --ask-pass --ask-become-pass SSH password: BECOME password[defaults to SSH password]:
PostgreSQL のコンテナを起動
Ansible からコンテナを起動できなかったため手動でコンテナを実行しました。
ansible-playbook コマンドによって2種類のイメージが作成されています。
ubuntu@ubuntu:~$ sudo docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE postgres 14.0-alpine 87440f4e7f9e 10 months ago 195MB psql14 v1.0 87440f4e7f9e 10 months ago 195MB
ポートやパスワードを指定してコンテナを起動します。
sudo docker run -p 5432:5432 --name psql14 -e POSTGRES_PASSWORD=postgres psql14:v1.0
PostgreSQL への接続を確認
psql コマンドを使用して接続を確認します。
Server [localhost]: 192.168.11.57 Database [postgres]: Port [5432]: Username [postgres]: Client Encoding [SJIS]: Password for user postgres: psql (12.10, server 14.0) WARNING: psql major version 12, server major version 14. Some psql features might not work. Type "help" for help.
Dockerfile で指定したバージョン PostgreSQL 14.0 に接続できていることが確認できました。
postgres=# SELECT version(); version -------------------------------------------------------------------------------------------------------------- PostgreSQL 14.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424, 64-bit (1 row)