Simplify unit file

Combine the flow for simple and oneshot services into one set of
execstart/execreload/execstop loops.

Change-Id: I2c250f7d0d14747b50fd77f54d3777c28f11e957
This commit is contained in:
Logan V 2018-03-20 23:35:34 -05:00
parent 237ee4694d
commit 134c6f7f24
3 changed files with 48 additions and 24 deletions

View File

@ -55,6 +55,10 @@ systemd_service_enabled: yes
# Set global service overrides used within the service unit file.
systemd_service_config_overrides: {}
# Systemd service type. Options include simple, forking, oneshot, etc.
# https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
systemd_default_service_type: simple
# The systemd services dictionary is a set of services that will be created. The dictionary
# can contain the following options:
# `service_name` -- (required) used to define the name of the service. This is typically the name of the executable.

View File

@ -7,35 +7,34 @@ After={{ item }}
{% endfor %}
[Service]
{% set service_type = item.service_type | default('simple') %}
{% set service_type = item.service_type | default(systemd_default_service_type) %}
Type={{ service_type }}
User={{ systemd_user_name }}
Group={{ systemd_group_name }}
{% if service_type == 'oneshot' and item.program_execstarts is defined %}
{% for execstart in item.program_execstarts %}
ExecStart={{ execstart }}
{% endfor %}
{% if item.program_execstops is defined %}
{% for execstop in item.program_execstops %}
ExecStop={{ execstop }}
{% endfor %}
{% endif %}
{% else %}
{% if item.program_override is defined %}
ExecStart={{ item.program_override }} {{ item.program_config_options | default('') }}
{% else %}
ExecStart={{ systemd_bin_path }}/{{ item.service_name }} {{ item.program_config_options | default('') }}
{% endif %}
{% if item.program_reload is defined and service_type != 'oneshot' %}
ExecReload={{ item.program_reload }}
{% else %}
ExecReload=/bin/kill -HUP $MAINPID
{% endif %}
{% if item.program_stop is defined %}
ExecStop={{ item.program_stop }}
{% endif %}
{% set _execstarts = item.execstarts | default(systemd_default_execstarts) %}
{% if _execstarts is string %}
{% set _execstarts = [_execstarts] %}
{% endif %}
{% for execstart in _execstarts %}
ExecStart={{ execstart }}
{% endfor %}
{% set _execreloads = item.execreloads | default(systemd_default_execreloads) %}
{% if _execreloads is string %}
{% set _execreloads = [_execreloads] %}
{% endif %}
{% for execreload in _execreloads %}
ExecReload={{ execreload }}
{% endfor %}
{% set _execstops = item.execstops | default(systemd_default_execstops) %}
{% if _execstops is string %}
{% set _execstops = [_execstops] %}
{% endif %}
{% for execstop in _execstops %}
ExecStop={{ execstop }}
{% endfor %}
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec={{ systemd_TimeoutSec }}

21
vars/main.yml Normal file
View File

@ -0,0 +1,21 @@
---
# Copyright 2018, Logan Vig <logan2211@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
systemd_default_execstarts:
- "{{ item.program_override | default(systemd_bin_path ~ '/' ~ item.service_name) }} {{ item.program_config_options | default('') }}"
systemd_default_execreloads:
- '/bin/kill -HUP $MAINPID'
systemd_default_execstops: "{{ item.program_stop | default([]) }}"