Check if LXC container OS version matches the host system on Debian

As part of my daily cron jobs I thought it would be nice to get an overview of which Debian version the system is running and if all LXC containers have a matching version. The script below will check this for you:

#!/bin/sh

SYSTEM_VERSION="$(cat /etc/debian_version)"
echo "System version: $SYSTEM_VERSION"
echo
CONTAINERS="`lxc-ls`"
for i in $CONTAINERS; do
   CONTAINER_VERSION="$(cat /var/lib/lxc/$i/rootfs/etc/debian_version)"
   if [ "$CONTAINER_VERSION" != "$SYSTEM_VERSION" ]; then
      echo " ❗️ $i: $CONTAINER_VERSION (upgrade required)"
   else
      echo " ✅ $i: $CONTAINER_VERSION"
   fi

done

Sample output:

System version: 11.5

 ✅ daapd: 11.5
 ✅ minecraft: 11.5
 ❗️ sonos: 10.13 (upgrade required)
 ✅ timemachine: 11.5