kavo’s diary

備忘録

ISUCON9予選問題のAnsible構築を試す

概要

isucon9-qualify/provisioning at master · isucon/isucon9-qualify · GitHubのプレイブックで練習環境を作ろうとしたメモ。

環境

ドキュメントに記載されている環境

今回試したこと

  • プラットフォーム:GCP
  • Ansible実行対象サーバ: Ubuntu 18.04 LTS (GCPで選べるubuntu-1804-bionic-v20200108)
  • Ansible実行元サーバ:GCP Cloud Shell(Debian系らしい。)

とにかくエラーにぶつかりまくったので色々試した。

ssh接続設定

  • プライベートIPを指定すると接続が失敗する。パブリックIPならOKだった。
  • ssh時のログイン名がrootかどうかの問題。
ssh-keygen -C "" -f anskey #anskey.pubの中身に「 root」を足し、GCPのVMインスタンスのSSH 認証鍵に貼って保存。
ssh -i anskey root@34.68.25.149 #デフォルト名だとダメ

GCP Cloud Shellからansible 2.2.1で試す。

下記のようにUTC設定するのに失敗したあたりでansibleが古いのが悪いのかなと思い、ansible 2.8.3を入れようとしたがpython関係のエラーで入らないので諦めた。

TASK [user.isucon : Create isucon group] ***************************************
fatal: [x.x.x.x]: FAILED! => {"changed": false, "failed": true, "msg": "groupadd: GID '1001' already exists\n", "name": "isucon"}
...ignoring

TASK [user.isucon : Create isucon user] ****************************************
fatal: [x.x.x.x]: FAILED! => {"changed": false, "failed": true, "msg": "Group ******** does not exist"}
...ignoring

TASK [user.isucon : Add sudoers] ***********************************************
ok: [x.x.x.x]

TASK [bootstrap : Install Dependencies] ****************************************
ok: [x.x.x.x]

TASK [bootstrap : Set timezone UTC] ********************************************
fatal: [x.x.x.x]: FAILED! => {"changed": true, "cmd": ["timedatectl", "set-timezone", "UTC"], "delta": "0:00:00.064019", "end": "2020-01-25 13:45:42.269964", "failed": true, "rc": 1, "start": "2020-01-25 13:45:42.205945", "stderr": "Failed to set time zone: Invalid time zone 'UTC'", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring

TASK [bootstrap : Setup xbuild] ************************************************
fatal: [x.x.x.x]: FAILED! => {"failed": true, "msg": "Failed to change ownership of the temporary files Ansible needs to create despite connecting as root.  Unprivileged become user would be unable to read the file."}

Ubuntuからansible 2.8.3で試す。

これで2.8.3が入る。aptを使うともっと古いのしか出てこない。

sudo apt-get update
sudo apt-get install make python-pip python-dev -y git
pip install PyYAML jinja2 paramiko
pip install packaging
git clone https://github.com/ansible/ansible.git -b v2.8.3 --depth 1
cd ansible/
sudo make install
ansible --version

hostsファイルでansible_ssh_userも指定しないと今のユーザでsshしようとするので指定する。

[web]
34.68.x.x ansible_ssh_user=root ansible_ssh_private_key_file=~/anskey

ansible疎通。

$ ansible -i hosts web -m ping
34.68.x.x | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
GID/UID衝突

GCPが追加したユーザがansibleで定義されている追加グループID、ユーザIDと衝突している。プレイブックを直すことで通過。

$ getent group
...
ubuntu:x:1000:datalab
google-sudoers:x:1001:datalab,root
datalab:x:1002:

$ cat /etc/passwd
...
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
datalab:x:1001:1002::/home/datalab:/bin/bash
initial.sqlがない(手順ミス)
fatal: [34.68.x.x]: FAILED! => {
    "changed": true, 
    "cmd": "mysql < initial.sql", 
    "delta": "0:00:00.002848", 
    "end": "2020-01-25 16:44:48.284014", 
    "invocation": {
        "module_args": {
            "_raw_params": "mysql < initial.sql", 
            "_uses_shell": true, 
            "argv": null, 
            "chdir": "/home/isucon/isucari/webapp/sql", 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "stdin": null, 
            "stdin_add_newline": true, 
            "strip_empty_ends": true, 
            "warn": true
        }
    }, 
    "msg": "non-zero return code", 
    "rc": 2, 
    "start": "2020-01-25 16:44:48.281166", 
    "stderr": "/bin/sh: 1: cannot open initial.sql: No such file", 
    "stderr_lines": [
        "/bin/sh: 1: cannot open initial.sql: No such file"
    ], 
    "stdout": "", 
    "stdout_lines": []
}

前準備の「初期データの作成」をやってなかった。あと「画像データの展開」も抜かしてたので、サイトの画像が出てなかった。 ローカル環境でISUCON9予選の問題を動かす : ISUCON公式Blog

「初期データの作成」「画像データの展開」を実施後再度ansibleかけて成功。無事画像が表示された。