Ansibleコンテナの復旧で結構手間取ってしまった

■はじめに

半年ぐらい前に諸般の理由でAnsibleコンテナを作ったのですが、今年の9月ぐらいからplaybookがこけるようになってました。

theboyalex.hatenablog.com

■というわけで色々アップデート

Python2系がサポート終了したのが関係してる?

2020年1月に2系のサポートが終了してたのですが、一応動いていたので放置していました。

でもなんか怪しいwarning出てるし直すか。。

[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host test.com should use /usr/bin/python3, but is using 
/usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

まずDockerfileを修正。
python3に諸々変更します。

FROM alpine:3.12.1

RUN set -x \
  && apk add --no-cache \
    sudo \
    bash \
    wget \
    python3 \
    py3-pip \
    openssh-client

RUN apk add --no-cache ansible

ついでにalpineのバージョンも上げておきます。

なんかplaybookでもpython関連で怒れらる

こんなこと言われるようになる。

TASK [推奨インストール] ****************************************************************
[WARNING]: Updating cache and auto-installing missing dependency: python-apt
changed: [test.com]

(中略)

TASK [dockerのインストール Ansible用] **************************************************

(中略)

stderr: pyrsistent requires Python '>=3.5' but the running Python is 2.7.17\n"

何故かpipがpython3じゃないよって言われる。
あと不思議なメッセージとしてpython-aptが読み込めないよって言われてる。どういうこと??

    - name: 推奨インストール 
      apt:
        name:
          - python-apt
          - linux-generic-hwe-18.04
        install_recommends: yes

(中略)

    - name: 基本インストール 
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg-agent
          - software-properties-common
          - python3-pip
        force: yes

恐らく2系のpipを使用している模様なので明示的にpip3を使うように基本インストールに加筆します。

あとpython-aptが読み込めない件は恐らく2系に紐づいてることが原因と仮定、正しいか分からないけどこちらも明示的にpython-aptを書いておきます。

interpreterの修正

再度実行すると別のエラー発生。
interpreterの指定が間違ってるよって言われる。

TASK [dockerのインストール Ansible用] **************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: No module named pkg_resources
fatal: [myselenoidvm3.eastus.cloudapp.azure.com]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (setuptools) on SelenoidVM's Python /usr/bin/python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

何じゃこれ?と思って調べたところ、そもそもpythoninterpreterを3系に明確に指定してやらねばならないとのこと。

こちらのページを参考にしました。感謝。

qiita.com

いくつかやり方があったけど、うちもansible.cfg用意してるので、こちらのdefaults句に書き込む方法で対応。

[defaults]
interpreter_python=/usr/bin/python3

docker-ce、docker-cliのバージョン変更

これでいくだろ!と思ったらまだエラーが出る。

TASK [docker再インストール] ***********************************************************
fatal: [test.com]: FAILED! => {"cache_update_time": 1607503728, "cache_updated": false, "changed": false, "msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force-confdef\" -o \"Dpkg::Options::=--force-confold\"      install 'docker-ce-cli=5:19.03.12~3-0~ubuntu-bionic'' failed: E: Packages were downgraded and -y was used without --allow-downgrades.\n", "rc": 100, "stderr": "E: Packages were downgraded and -y was used without --allow-downgrades.\n", "stderr_lines": ["E: Packages were downgraded and -y was used without --allow-downgrades."], "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following package was automatically installed and is no longer required:\n  linux-headers-4.15.0-124\nUse 'sudo apt autoremove' to remove it.\nThe following packages will be DOWNGRADED:\n  docker-ce-cli\n0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 1 not upgraded.\n", "stdout_lines": ["Reading package lists...", "Building dependency tree...", "Reading state information...", "The following package was automatically installed and is no longer required:", "  linux-headers-4.15.0-124", "Use 'sudo apt autoremove' to remove it.", "The following packages will be DOWNGRADED:", "  docker-ce-cli", "0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 1 not upgraded."]}

ダウングレードしたつもりはないのだけど、まぁ要するにバージョン古いってことで、最新のバージョンに変更。

docker公式にはコマンド叩いてバージョン調べる方法が書いてあったり、latestの指定方法なんかも記述してありますが、今回はコマンドではなく下記ページを参考にしてます。

https://ubuntu.pkgs.org/18.04/docker-ce-stable-amd64/

    - name: docker再インストール
      apt:
        name:
          - docker-ce=5:20.10.0~3-0~ubuntu-bionic
          - docker-ce-cli=5:20.10.0~3-0~ubuntu-bionic
          - containerd.io

実行します。

ansible % docker container run -itd --name ansible test/ansible:0.1                       
e13d6585f8e0b686d2fea62e4191977c68aa0d8fc55d54cca3adcfebb747fa54
ansible % docker container logs -f ansible   

(中略)

PLAY RECAP *********************************************************************
test.com : ok=17   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

おし、成功ですね!

いやー作成当初の記憶がすっかり薄れてて、何でここってこう書いてたんだっけ?って思い出しながら修正したので意外と手間取ってしまった。。


本日はここまで。

ではでは。