てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] ansible-core 2.17.0 で マネージドノード側の Python 2 系全部と Python 3.6 のサポートがなくなった

はじめに

いよいよ、という感じですが、ansible-core 2.17.0 で、マネージドノード(自動化対象)側での Python 2系と Python 3.6 のサポートがなくなりました。

テスト対象からも外されているため、サポートされているバージョンよりバグが起こる可能性が大きいでしょうし、バージョンに起因するバグの場合はissue も対応されなくなっていると思います。

Releases and maintenance — Ansible Community Documentation

2.17 の Target Python 欄に Python 2 系がなくなった

Changelog

2.17.0 の Chaneglog には以下の記載があります。

Removed Python 2.7 and Python 3.6 as a supported remote version. Python 3.7+ is now required for target execution.

少しややこしいですが、ここの「target execution」は managed node (マネージドノード: 自動化対象)のことです。

記載の通り Python 3.6 のサポートもなくなりました。デフォルトが Python 3.6 の Linux ディストリビューションのバージョンもあった気がします。

Python 2 系の環境に対しておためし(エラー)

デフォルト Python 2.6.6 のままの CentOS 6.8 の環境に ansible-core 2.17.0 から接続してみます。 (CentOS 6 はだいぶ古いです。ここでは本記事の検証用途なので、利用を推奨する意図はありません。)

さくっと、アドホックコマンドで試します。

% ansible -i inventory.ini centos6 -m ping     
[WARNING]: No python interpreters found for host centos6 (tried ['python3.12', 'python3.11', 'python3.10',
'python3.9', 'python3.8', 'python3.7', '/usr/bin/python3', 'python3'])
centos6 | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "module_stderr": "Shared connection to 192.168.1.141 closed.\r\n",
    "module_stdout": "/bin/sh: /usr/bin/python3: そのようなファイルやディレクトリはありません\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 127
}

この現象は Python 2のサポートがなくなり、Interpreter Discovery で探す Python のパスとしても python2 系のパスを探さなくなったことによるものです。最終的には /usr/bin/python3 がないというエラーになっています。

では、次にインタープリターのパスを、マネージドノード上に実際にあるパス(Python2)を指定するとどうでしょうか。

 % ansible -i inventory.ini centos6 -m ping -e ansible_python_interpreter=/usr/bin/python
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: SyntaxError: invalid syntax
centos6 | FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to 192.168.1.141 closed.\r\n",
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/sakana/.ansible/tmp/ansible-tmp-1718715820.50483-30749-84959101405755/AnsiballZ_ping.py\", line 107, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/sakana/.ansible/tmp/ansible-tmp-1718715820.50483-30749-84959101405755/AnsiballZ_ping.py\", line 99, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/sakana/.ansible/tmp/ansible-tmp-1718715820.50483-30749-84959101405755/AnsiballZ_ping.py\", line 44, in invoke_module\r\n    from ansible.module_utils import basic\r\n  File \"/tmp/ansible_ping_payload_4nYFEq/ansible_ping_payload.zip/ansible/module_utils/basic.py\", line 17\r\n    msg=f\"ansible-core requires a minimum of Python version {'.'.join(map(str, _PY_MIN))}. Current version: {''.join(sys.version.splitlines())}\",\r\n                                                                                                                                               ^\r\nSyntaxError: invalid syntax\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

Python の構文レベルでのエラーになりました。これは初見だと理解が難しいかもしれませんね。

(おまけ) ansible-core 2.16 だと

2.16 では、Python 2系としてぎりぎりPython 2.7 がサポートされていました。2.16 で Python 2.6 の環境に実行すると、以下のように親切なエラーになりました。

%  ansible -i inventory.ini centos6 -m ping 
centos6 | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "msg": "ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.6. Current version: 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]"
}

これまでエラーハンドリングができていたところが、Python2 系のサポートがバッサリなくなったことで、ハンドリングもできなくなったのだと思います。

おわりに

開発に携わっている方の目線に立つと、Pyrhon 2 対応のコードが一式消せて、今後の負担もかるくなったのではないでしょうか。

INTERPRETER_PYTHON_DISTRO_MAP の実質的な廃止の件を含め、ansible-core 2.17 は Pythonインタプリター周りの変更が大きめな印象です。

これまで、どのインタープリタが利用されていたか意識せずなんとなく動いていた環境は、このタイミングで注意が必要になったと思います。

参考

関連PR: Drop Python 2.7 and Python 3.6 support by mattclay · Pull Request #81866 · ansible/ansible · GitHub

また、Python 2 のサポートがなくなった影響で、ansible.builtin.yum モジュールがなくなりました

tekunabe.hatenablog.jp