From 62bd24859e96ef28350911ede44480864ac6d161 Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Fri, 14 Oct 2022 12:11:06 +0200 Subject: [PATCH] Allow to define pre-start and post-stop tasks WHile you can leverage config_overrides for that, it might become tricky if you need to define more then 1 pre/post exec tasks. Change-Id: I3d8b36f5e9f4be89eaff848d8ba88a38ce5285e4 --- defaults/main.yml | 4 ++++ .../systemd_service_pre_post_exec-283b657e52d33d2b.yaml | 6 ++++++ templates/systemd-service.j2 | 8 ++++++++ 3 files changed, 18 insertions(+) create mode 100644 releasenotes/notes/systemd_service_pre_post_exec-283b657e52d33d2b.yaml diff --git a/defaults/main.yml b/defaults/main.yml index a5ef5a1..b55852d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -102,8 +102,10 @@ systemd_environment: {} # can contain the following options: # `service_name` -- (required) used to define the name of the service. This is typically the name of the executable. # `service_type` -- (optional) Set the service type, default is "simple". +# `execstartpres` -- (optional) Set the program that wil lexecute before service startup. # `execstarts` -- (required) Set the program to start, when the service is simple the list has a max length of 1. # `execstops` -- (optional) Set the program to stop, when the service is simple the list has a max length of 1. +# `execstopposts` -- (optional) Set the program that wil lexecute after service stop. # `after_targets` -- (optional) Start the service after this list of dependency units. # `partof` -- (optional) Set the systemd "partof" directive. This ties a service unit to another. # `config_overrides` -- (optional) This allows any section or key=value pair to be set within the systemd unit file. @@ -198,6 +200,8 @@ systemd_environment: {} # execstops # - /usr/bin/stopcmd1 # - /usr/bin/stopcmd2 +# execstopposts: +# - /usr/local/bin/hook # sockets: # - socket_name: SocketServiceZ # after_targets: diff --git a/releasenotes/notes/systemd_service_pre_post_exec-283b657e52d33d2b.yaml b/releasenotes/notes/systemd_service_pre_post_exec-283b657e52d33d2b.yaml new file mode 100644 index 0000000..1219ef7 --- /dev/null +++ b/releasenotes/notes/systemd_service_pre_post_exec-283b657e52d33d2b.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Now you can define ``execstartpres`` and ``execstopposts`` keys for the + `systemd_services` structure. They will allow to define pre-start and + post-stop service executables and must be defined as lists. diff --git a/templates/systemd-service.j2 b/templates/systemd-service.j2 index 83abd1e..6c92163 100644 --- a/templates/systemd-service.j2 +++ b/templates/systemd-service.j2 @@ -32,6 +32,10 @@ Environment="{{ key }}={{ value }}" EnvironmentFile={{ item.environment_file | default(systemd_environment_file) }} {% endif %} +{% for execstartpre in item.execstartpres | default([]) %} +ExecStartPre={{ execstartpre }} +{% endfor %} + {% set _execstarts = item.execstarts %} {% if _execstarts is string %} {% set _execstarts = [_execstarts] %} @@ -56,6 +60,10 @@ ExecReload={{ execreload }} ExecStop={{ execstop }} {% endfor %} +{% for execstoppost in item.execstopposts | default([]) %} +ExecStopPost={{ execstoppost }} +{% endfor %} + # Give a reasonable amount of time for the server to start up/shut down TimeoutSec={{ systemd_TimeoutSec }} {% if service_type != 'oneshot' %}