From a7fb205256c0773e5911f77513b02bf190453c0f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Wed, 20 Jan 2021 11:38:46 +0100 Subject: [PATCH] Add a base role Signed-off-by: Pierre-Yves Chibon --- .../roles/base/files/conditional-reload.sh | 26 +++++++++++++++++++ .../roles/base/files/conditional-restart.sh | 10 +++++++ ansible/roles/base/tasks/main.yml | 17 ++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 ansible/roles/base/files/conditional-reload.sh create mode 100644 ansible/roles/base/files/conditional-restart.sh create mode 100644 ansible/roles/base/tasks/main.yml diff --git a/ansible/roles/base/files/conditional-reload.sh b/ansible/roles/base/files/conditional-reload.sh new file mode 100644 index 0000000..988a08b --- /dev/null +++ b/ansible/roles/base/files/conditional-reload.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# reload SERVICE only if PACKAGE is installed. +# We use this throughout handlers/restart_services.yml + +SERVICE=$1 +PACKAGE=$2 + +rpm -q $PACKAGE + +INSTALLED=$? + +if [ $INSTALLED -eq 0 ]; then + echo "Checking if $SERVICE is running" + /sbin/service $SERVICE status >& /dev/null + if [ $? == 0 ]; then + echo "Package $PACKAGE installed and running. Attempting reload of $SERVICE." + /sbin/service $SERVICE reload + exit $? # Exit with the /sbin/service status code + fi + echo "Package $PACKAGE is install, but $SERVICE is not running, skipping..." + exit 0 +fi + +# If the package wasn't installed, then pretend everything is fine. +echo "Package $PACKAGE not installed. Skipping reload of $SERVICE." +exit 0 diff --git a/ansible/roles/base/files/conditional-restart.sh b/ansible/roles/base/files/conditional-restart.sh new file mode 100644 index 0000000..8da52dc --- /dev/null +++ b/ansible/roles/base/files/conditional-restart.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# +# We use this to try and restart a service. +# If it's not running, do nothing. +# If it is running, restart it. +# + +SERVICE=$1 +# Check if service unit is present before trying to restart it +/usr/bin/systemctl cat $1.service &>/dev/null && /usr/bin/systemctl try-restart $1 || true diff --git a/ansible/roles/base/tasks/main.yml b/ansible/roles/base/tasks/main.yml new file mode 100644 index 0000000..bfed18e --- /dev/null +++ b/ansible/roles/base/tasks/main.yml @@ -0,0 +1,17 @@ +- name: Install packages that are useful in general + package: name={{ item }} state=present + with_items: + - vim + - tmux + tags: + - base + - package + +- name: Install the conditional-restart/reload scripts + copy: src={{ item }} dest=/usr/local/bin + owner=root group=root mode=0755 + with_fileglob: + - conditional* + tags: + - config + - base