From a9235f0e2d82beef68c4d29c731f0dc97159efab Mon Sep 17 00:00:00 2001 From: Salt Date: Wed, 10 Mar 2021 10:41:40 -0600 Subject: [PATCH] Add quick MOTD for docker containers --- playbooks/pimp.yml | 2 ++ roles/motd/defaults/main.yml | 4 ++++ roles/motd/templates/motd.sh | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/playbooks/pimp.yml b/playbooks/pimp.yml index 9389f49..58ea582 100755 --- a/playbooks/pimp.yml +++ b/playbooks/pimp.yml @@ -81,6 +81,8 @@ motd_watch_services_extra: - docker - isc-dhcp-server + motd_watch_containers_extra: + - unbound tags: [ pis, mp, motd ] # And also serving DHCP - hosts: pi-mp-1.desu.ltd diff --git a/roles/motd/defaults/main.yml b/roles/motd/defaults/main.yml index c563e06..8333dfd 100644 --- a/roles/motd/defaults/main.yml +++ b/roles/motd/defaults/main.yml @@ -22,3 +22,7 @@ motd_watch_services: - ansible-pull - backup motd_watch_services_extra: [] +# Docker images to look for. Matches a simple glob (*{{ item }}*) +# If Docker is not running, this section will be omitted +motd_watch_containers: [] +motd_watch_containers_extra: [] diff --git a/roles/motd/templates/motd.sh b/roles/motd/templates/motd.sh index f920638..f560b49 100755 --- a/roles/motd/templates/motd.sh +++ b/roles/motd/templates/motd.sh @@ -13,6 +13,11 @@ declare -a services services+=("{{ item }}") {% endfor %} +declare -a containers +{% for item in (motd_watch_containers + motd_watch_containers_extra)|sort %} +containers+=("{{ item }}") +{% endfor %} + ## Now, we actually put this info to use # Starting with services if [ -n "${services[*]}" ]; then @@ -43,6 +48,24 @@ if [ -n "${services[*]}" ]; then done fi +# Containers, if docker is running +if [ -n "${containers[*]}" ] && systemctl -q is-active docker; then + printf "\e[1mContainer Statuses\e[0m\n" + len=20 + for container in "${containers[@]}"; do + status="\e[33mUnknown\e[0m" + image="$(docker ps | tail -n +2 | awk '{print $2}' | grep -ie "$container")" + if [ -n "$image" ]; then + status="\e[1;32mRunning\e[0m - $image" + fi + if [ -z "$image" ]; then + status="\e[1;31mNot Running\e[0m" + fi + printf " * \e[37m%-${len}.${len}s\e[0m - $status " "$container" + printf "\n" + done +fi + ## And some generic system status stuff printf "\e[1mSystem Status\e[0m\n" if [ -f /var/run/reboot-required ]; then