Posted by virantha on Sat 30 March 2019

Setting up a Raspberry Pi with Python 3.7+ using Ansible

Here's a set of Ansible playbook files to do an unattended compile of Python 3.7+ with SSL on a headless wifi-connected Raspberry Pi. A recent project of mine (bricknil) uses 3.7, and it was a little frustrating to get the default 3.5 upgraded on my Raspbian image -- I had to compile Python from source with the newer SSL libraries (so that pip works), and get around some uuid compilation issues that took a fair bit of web research to figure out.

Everything is run from the local machine talking to the Pi over ssh, and you just need Ansible installed on your local machine (which can be as easy as pip install ansible depending on your OS). Hope this helps save some time for anyone else needing get modern Python installed on their RPi!

1   Steps

1.1   Download a Raspbian image and burn it to a SD card

  1. Download the Raspbian Stretch Lite image

  2. Burn it to a suitable SD card. On OS X, Balena Etcher is a good tool to do this.

  3. On the newly imaged SD card's /boot folder:

    1. Make an empty file called ssh to enable SSH access.

    2. Make wpa_supplicant.conf file to supply your wifi credentials:

      ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
            psk="WIFI PASSWORD"
  4. Now, you should be able to use this SD card to boot your raspberry pi. After giving it a few minutes to boot, you can login in by doing the following on your local machine:

    ssh pi@raspberrypi.local
    <enter 'raspberry' as the default password>

1.2   Install Ansbile on your local machine

You can just type pip install ansible

1.3   Change the default password on the Pi

  1. You may need to modify the hosts file in pi_setup directory to put in your own IP address for the Pi.

  2. You will need to install the python library passlib by running pip install passlib on your local machine (not the Pi). This is needed to hash the new password.

  3. Use the change_password.yml playbook to test your ansible install and change the default password on the pi (use raspberry for the ssh password when prompted, and then enter your new password):

    ansible-playbook -i hosts change_password.yml --ask-pass

    The playbook being run is below, and just prompts the user for a new password before making a copy of the shadow file and updating the password:

1.4   Run the setup playbook

  1. The final step is to just run the the install playbook. This will install all the packages, download and compile OpenSSL, followed by Python 3.7, and set up a virtualenv. The actual playbook is in tasks.yml, shown below and included in the source, and the command to execute it on your local machine is:

    ansible-playbook -i hosts tasks.yml --ask-pass

    The install will take around two hours, so please be patient. Most of the time is spent compiling and installing Python from source. Here's the playbook being executed:

    At the end, there are a couple of steps where I install a sample package and clone a github repo (which in this case is my bricknil project for controlling Lego trains and robots. Python 3.7 will be available as /usr/local/bin/python3.7 and virtualenvwrapper will use it as the default python.

  2. And that's it! Your raspberry pi should be ready to go as a networked appliance with an up-to-date Python.

© Virantha Ekanayake. Built using Pelican. Modified svbhack theme, based on theme by Carey Metcalfe