Catalogue
Specifying Python3 on Ansible Target Hosts to Run pip install

Specifying Python3 on Ansible Target Hosts to Run pip install

🌐 日本語で読む

Overview

On a Raspberry Pi Zero WH, running python -V returned Python 2.7.16.

The Raspberry Pi setup is managed with Ansible, so I was wondering whether I should
unlink python and ln -s /usr/bin/python3 /usr/bin/python via Ansible.
But it turned out there was a much simpler way, so here’s a quick note.

OS Version Information

1
2
3
4
5
6
7
8
9
10
11
12
$ cat /etc/os-release

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

The Thumbnail Says It All, but Just in Case

The following ansible.cfg setting is all there is to it.

  • ansible.cfg
1
2
[defaults]
interpreter_python=/usr/bin/python3
  • tasks/main.yml
1
2
3
4
5
# NOTE: use it as pip3
- name: pip3 install packages
pip:
name:
- mh_z19

After running ansible-playbook:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// python2 is linked to /usr/bin/python
$ ls -al /usr/bin/python
lrwxrwxrwx 1 root root 7 Mar 6 15:22 /usr/bin/python -> python2

$ python -V
Python 2.7.16

// confirm that the module is not installed for python2
// pip list would also work, but this gives a clearer message, so I use this
$ sudo python -m mh_z19
/usr/bin/python: No module named mh_z19

$ python3 -V
Python 3.7.3

// confirm that the module is installed for python3
$ sudo python3 -m mh_z19
{"co2": 695}

Without bothering to switch the linked python, I was able to simply specify the Python version and use it!

That’s all.
I hope this is helpful.

kenzo0107

kenzo0107