Useful Ansible plays for Raspberry Pis

I run a small cluster of 4 Raspberry Pis at home for experimenting. Apart from containerizing certain things, there are routine maintainance operations like updating installed packages or monitoring ram/disk usage that I have some simple Ansible playbooks for.

Quick setup notes:

  • I'm not diving deep on Ansible here. Just sharing some things I use. I'm assuming some knowledge of what Ansible is and how it works.
  • Here's a quick setup snippet on how I have 4 Rapsberry Pis grouped and their names. Note: I have ssh keys set up in ~/.ssh/config and I'm VPN'd to each of these via Tailscale. So no key setup in this post and the VPN is how I can connect directly to these hostnames.

ansible.cfg

1[defaults] 2inventory = pis.ini 3stdout_callback = yaml

pis.ini

1[pis] 2raspberrypi-1 3raspberrypi-2 4raspberrypi-3 5raspberrypi-4

Ok, now on to the first playbook, which I use to keep Pi OS up to date:

pis_update.yml

1--- 2- name: Update Installed Packages 3 hosts: pis 4 become: true 5 tasks: 6 - name: Update apt cache 7 apt: 8 update_cache: true 9 cache_valid_time: 86400 10 11 - name: Upgrade all packages to the latest version 12 apt: 13 upgrade: dist 14 autoremove: true 15 autoclean: true

The two tasks here are easy to follow - update the apt cache and then upgrade all installed packages.

Next I have a nice way to get reports on each of my Pis, but one quick setup playbook before we do that:

pis_setup.yml

1--- 2- name: Install speedtest-cli 3 hosts: pis 4 become: true 5 tasks: 6 - name: Install speedtest-cli 7 ansible.builtin.apt: 8 name: speedtest-cli 9 state: present

Installing speedtest-cli will give us a chance to run speed tests for each of the Pis in the cluster.

And now the playbook for gathering reports:

pis_report.yml

1--- 2- name: Disk usage report 3 hosts: pis 4 become: yes 5 tasks: 6 - name: Get disk usage 7 ansible.builtin.setup: 8 gather_subset: 9 - "!all" 10 - "min" 11 filter: "ansible_mounts" 12 - name: Print disk usage 13 ansible.builtin.debug: 14 msg: | 15 {% for mount in ansible_mounts %} 16 Mount: {{ mount.mount }} 17 Total Size: {{ mount.size_total | human_readable }} 18 Free Space: {{ mount.size_available | human_readable }} 19 Used Space: {{ (mount.size_total - mount.size_available) | human_readable }} 20 {% endfor %} 21 22- name: Memory report 23 hosts: pis 24 become: true 25 tasks: 26 - name: Get memory report 27 command: free -h -t --giga 28 register: free_memory 29 - name: Print memory report 30 debug: 31 msg: "{{ free_memory.stdout_lines }}" 32 33- name: Pi models report 34 hosts: pis 35 become: yes 36 tasks: 37 - name: Get Pi Models 38 shell: cat /proc/device-tree/model | tr -d '\0' 39 register: pi_model 40 - name: Print Pi Models 41 debug: 42 msg: "{{ pi_model.stdout_lines }}" 43 44- name: OS versions report 45 hosts: pis 46 gather_facts: true 47 tasks: 48 - name: Get OS Version 49 command: cat /etc/os-release 50 register: os_release 51 - name: Extract PRETTY_NAME 52 set_fact: 53 pretty_name: "{{ os_release.stdout | regex_search('PRETTY_NAME=(.*)', '\\1') }}" 54 - name: Print OS Version 55 debug: 56 msg: "{{ pretty_name }}" 57 58- name: Speed test report 59 hosts: pis 60 become: false 61 tasks: 62 - name: Execute speedtest-cli 63 ansible.builtin.command: speedtest-cli --simple 64 register: speedtest_result 65 66 - name: Print speed test results 67 ansible.builtin.debug: 68 msg: "{{ speedtest_result.stdout }}"

We have the following five plays running here:

  • Disk usage report
  • Memory report
  • Raspberry Pi models report
  • OS versions report
  • Speed test report

I have my Raspberry Pis named raspberrypi-1, raspberrypi-2, raspberrypi-3, and raspberrypi-4, which each show up in each of these reports. I've also recently reset these so the memory and disk usage will both be low in these reports.

For Disk usage report:

1ok: [raspberrypi-1] => 2 msg: |- 3 Mount: / 4 Total Size: 14.32 GB 5 Free Space: 7.39 GB 6 Used Space: 6.93 GB 7 Mount: /boot 8 Total Size: 254.99 MB 9 Free Space: 222.27 MB 10 Used Space: 32.72 MB 11ok: [raspberrypi-2] => 12 msg: |- 13 Mount: / 14 Total Size: 28.97 GB 15 Free Space: 22.14 GB 16 Used Space: 6.83 GB 17 Mount: /boot 18 Total Size: 254.99 MB 19 Free Space: 223.07 MB 20 Used Space: 31.91 MB 21ok: [raspberrypi-3] => 22 msg: |- 23 Mount: / 24 Total Size: 28.97 GB 25 Free Space: 22.16 GB 26 Used Space: 6.81 GB 27 Mount: /boot 28 Total Size: 254.99 MB 29 Free Space: 223.18 MB 30 Used Space: 31.80 MB 31ok: [raspberrypi-4] => 32 msg: |- 33 Mount: / 34 Total Size: 28.97 GB 35 Free Space: 21.40 GB 36 Used Space: 7.57 GB 37 Mount: /boot 38 Total Size: 254.99 MB 39 Free Space: 222.90 MB 40 Used Space: 32.09 MB

For Memory report:

1ok: [raspberrypi-1] => 2 msg: 3 - ' total used free shared buff/cache available' 4 - 'Mem: 909M 183M 198M 0.0K 526M 661M' 5 - 'Swap: 99M 38M 61M' 6 - 'Total: 1.0G 222M 260M' 7ok: [raspberrypi-2] => 8 msg: 9 - ' total used free shared buff/cache available' 10 - 'Mem: 909M 148M 184M 0.0K 576M 696M' 11 - 'Swap: 99M 10M 89M' 12 - 'Total: 1.0G 159M 273M' 13ok: [raspberrypi-3] => 14 msg: 15 - ' total used free shared buff/cache available' 16 - 'Mem: 909M 147M 193M 0.0K 568M 697M' 17 - 'Swap: 99M 9.0M 90M' 18 - 'Total: 1.0G 156M 284M' 19ok: [raspberrypi-4] => 20 msg: 21 - ' total used free shared buff/cache available' 22 - 'Mem: 1.8G 168M 592M 0.0K 1.1G 1.6G' 23 - 'Swap: 99M 1.0M 98M' 24 - 'Total: 1.9G 170M 691M'

For Pi models report

1ok: [raspberrypi-1] => 2 msg: 3 - Raspberry Pi 3 Model B Rev 1.2 4ok: [raspberrypi-2] => 5 msg: 6 - Raspberry Pi 3 Model B Rev 1.2 7ok: [raspberrypi-3] => 8 msg: 9 - Raspberry Pi 3 Model B Rev 1.2 10ok: [raspberrypi-4] => 11 msg: 12 - Raspberry Pi 4 Model B Rev 1.1

For OS versions report:

1ok: [raspberrypi-1] => 2 msg: 3 - '"Debian GNU/Linux 11 (bullseye)"' 4ok: [raspberrypi-2] => 5 msg: 6 - '"Debian GNU/Linux 11 (bullseye)"' 7ok: [raspberrypi-3] => 8 msg: 9 - '"Debian GNU/Linux 11 (bullseye)"' 10ok: [raspberrypi-4] => 11 msg: 12 - '"Debian GNU/Linux 11 (bullseye)"'

For Speed test report:

1ok: [raspberrypi-1] => 2 msg: |- 3 Ping: 23.252 ms 4 Download: 5.31 Mbit/s 5 Upload: 5.20 Mbit/s 6ok: [raspberrypi-2] => 7 msg: |- 8 Ping: 26.886 ms 9 Download: 4.24 Mbit/s 10 Upload: 3.54 Mbit/s 11ok: [raspberrypi-3] => 12 msg: |- 13 Ping: 21.114 ms 14 Download: 12.47 Mbit/s 15 Upload: 8.30 Mbit/s 16ok: [raspberrypi-4] => 17 msg: |- 18 Ping: 23.271 ms 19 Download: 22.04 Mbit/s 20 Upload: 9.84 Mbit/s

I think it's a cool little playbook for gathering info across a cluster of Raspberry Pis. Also interesting to note the speed jump on raspberrypi-4, which is the only model 4 of the cluster. The rest are 3s.


Previous: Drawing waveforms using AudioKit and CoreGraphics