
最初に
最近はPythonについて、Python 2については今後のアップデートがないため事情がない限りはPython 3を使用することが推奨されていると思います。
とはいえ、Debian 10 (Buster)についてはリリースの関係からpythonのデフォルトは2で設定されており、 python3.x系列を利用したい場合には明示的にpython3
を実行する必要があります。

第581回 Debian 10 "Buster"の紹介 | gihyo.jp
去る2019年7月6日に、Ubuntuの母であるDebianの新しいリリースであるDebian 10(コードネーム"Buster")がリリースされました。今回のUbuntu Weekly Recipeでは、このDebian 10 "Buster"について紹介します。
しかしながらAnsibleをDebian 10なマシンに対して実行すると以下のようなメッセージが表示されます。
PLAY [all] *******************************************************************************************************************
TASK [apt install packages] **************************************************************************************************
[WARNING]: Platform linux on host xxx.xxx.xxx.xx2 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.
ok: [xxx.xxx.xxx.xx2]
[WARNING]: Platform linux on host xxx.xxx.xxx.xx1 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.
ok: [xxx.xxx.xxx.xx1]
というわけで実行するPythonのバージョンを指定する方法をメモしておこうと思います。
確認環境
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ uname -mr
4.19.85-mvebu64 aarch64
予めDebianのデフォルトで使用するバージョンを変更する
update-alternatives --install /usr/bin/python python /usr/bin/python3 1
update-alternatives --install /usr/bin/python python /usr/bin/python2 2
update-alternatives --set python /usr/bin/python3
設定できていると以下のような感じになっているかと思います。
$ sudo update-alternatives --config python
[sudo] password for kometch:
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python2 2 auto mode
1 /usr/bin/python2 2 manual mode
* 2 /usr/bin/python3 1 manual mode
Press <enter> to keep the current choice[*], or type selection number:
update-alternatives(8) - Linux man page
alternatives creates, removes, maintains and displays information about the symbolic links comprising the alternatives system.
Ansibleでサクッと設定する場合
とりあえず自分は以下のようなplaybookを用意して設定するようにしています。
---
- hosts: all
become: True
tasks:
- name: set python3 as default
shell: |
update-alternatives --install /usr/bin/python python /usr/bin/python3 1
update-alternatives --install /usr/bin/python python /usr/bin/python2 2
update-alternatives --set python /usr/bin/python3
when:
ansible_distribution == "Debian"
Ansible側で実行するPythonを指定する
Ansibleで明示的にPython3で実行するようにするにはansible.cfg
の[defaults]
セクションにinterpreter_python=/usr/bin/python3
を設定します。
[defaults]
ansible_python_interpreter=/usr/bin/python3
このことについては最初のlogにも記載がありますが、公式ドキュメントに専用のページが用意されています。
ちなみに
あとはpyenv(Virtual Environment)
で設定する方法もあります。
参考
Python Default Version - Raspberry Valley
How To Install Python 3 and Set Up a Programming Environment on Debian 10 | DigitalOcean
Python is a flexible and versatile programming language suitable for many use cases, including scripting, automation, data analysis, machine learning, and ba…