Raspberry Pi Configuration Management

In this post we will be looking at setting up our Raspberry Pi in an automated fashion so that we can install Apache Airflow and some other tools to automate key parts of our workflow.

Purchasing a Raspberry Pi

I recommend the Canakit Raspberry Pi 4 4Gb Starter Kit.

It contains everything you need to get a complete setup going quickly, and it is available for a very reasonable price.

The kit contains:

  • The Raspberry Pi 4 itself (with 4GB of RAM)
  • A 32GB SD card, preloaded with a bootloader (no fussing about flashing the drive yourself)
  • A minimal, but attractive case
  • A cooling fan and additional heat sinks for the CPU, RAM and GPU chips
  • A USB type-C powerswitch adapter (actually quite useful)
  • AC Power adapter (to USB type-C)
  • A 4K compatible micro-HDMI to HDMI cable

Setting up the Raspberry Pi & Operating System

Here is a Youtube video I’ve put to together explaining how to assemble and setup a Canakit Raspberry Pi 4.


Choosing a configuration tool

Options are:

  • Chef

    • Familiar
    • Resource intensive (due to Ruby)
    • Ruby syntax
    • Solo and server-based architecture
  • Ansible

    • Industry leading
    • Solo and server-base architecture
    • Easy to learn
  • Puppet

    • Has lost favour in recent years
    • Hard to learn

Our choice:

Ansible

Has great Terraform support.

Less resource-heavy which is important on a Raspberry Pi.


SSHing Into a Raspberry Pi

By default you can log into the Raspberry Pi via SSH credentials like so: ssh pi@raspberrypi.local Then when prompted for a password, you can enter the default password of raspberry.

This may also be set to your admin password if you changed that after installation. If it is still the default, this password should be changed immediately.

If you need help setting your SSH password, you can read more here.

You can also look into setting up a *.local domain name to make things easier if you’d like.


Installing Ansible on a Raspberry Pi

Here we install some required packages (sshpass and dirmmngr), add a PPA which gives us a more recent version of Ansible. Then we add keys for this new PPA, update our local apt, then finally download and install the latest Ansible.

echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" \
  | sudo tee -a /etc/apt/sources.list
  
sudo apt-key adv \
  --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
  
sudo apt-get update

sudo apt-get install -y \
  ansible sshpass dirmngr

ansible --version

Installing Ansible on Our Local Machine

Now on our local machine, we should also ensure we have Ansible installed.

On MacOS I can do this simply using HomeBrew and running the command brew install ansible.

Other installation methods are available here.

Then let’s create our hosts file (inventory) so Ansible knows where to find our Raspberry Pi.

# Run this in a terminal on your local machine
sudo mkdir /etc/ansible
sudo nano /etc/ansible/hosts

Enter the following configuration.

# /etc/ansible/ansible.cfg
[defaults]
inventory=./hosts

Note: You can also update your Ansible configuration to look in the local directory for the inventory file. This will allow you to have multiple projects using different configurations in the future. Read how here.


Setting Up an SSH Key on the Raspberry Pi

First of all you will need to generate a SSH key key which is usually found in your home directory at the following location. If you do not have one of these keys you can learn how to generate one here.

Next will need to copy our public key to the raspberry pi using the following:

ssh-copy-id -i ~/.ssh/id_rsa.pub pi@raspberrypi.local

You should then be asked to enter your raspberry pi’s SSH password, and should copy your public key onto the device. This will mean that when logging in from your current machine, you should no longer require a password.

This means that Ansible also will not require a password.


Setting Up a Basic Ansible Configuration

Here I’m going to define my Raspberry Pi as the shorthand “pi”, add it to a group called “automaton” and add a variable to all hosts within that group.

# /etc/ansible/hosts
[pi]
pi@raspberrypi.local

[automaton:children]
pi

[automaton:vars]
cy_automaton=true

Now let’s test our configuration so far by running a command on our Raspberry Pi remotely.

ansible pi -a ls

You should see a directory listing of your Raspberry Pi’s home directory.

Yay!

In a followup post we’ll be looking at installing Apache Airflow and other automation tools on the Raspberry Pi using Ansible.

This will allow us to automatically publish blog posts, tweets, and by doing nothing more than dropping a file in a directory.

Stay tuned, and as always, take it easy kittens!

Meow!


Further Viewing

Further Reading


comments powered by Disqus