Add service restart handler

A global var systemd_service_restart_changed is added which toggles
restart on unit file change for all systemd_services.

A systemd_services var 'restart_changed' is added to toggle restarts
on a per-service basis.

systemd daemon is not reloaded in a loop anymore, it is only
reloaded once, in a handler, when unit files change.

Consumer roles of systemd_service can listen to the handler
'systemd service changed' to implement additional orchestration when
service units are modified.

Change-Id: I803bca638c30696004f934ad1e28920786948f38
This commit is contained in:
Logan V 2018-03-20 22:56:49 -05:00
parent 7745439f35
commit 72efb59ad5
4 changed files with 39 additions and 2 deletions

View File

@ -17,6 +17,9 @@ systemd_user_name: root
systemd_group_name: root systemd_group_name: root
systemd_slice_name: system systemd_slice_name: system
# Restart services when a change occurs
systemd_service_restart_changed: yes
# This is the prefix used for all temp files of a given type. # This is the prefix used for all temp files of a given type.
systemd_tempd_prefix: tempd systemd_tempd_prefix: tempd
@ -88,6 +91,7 @@ systemd_default_service_type: simple
# - '/usr/bin/ServiceY' # - '/usr/bin/ServiceY'
# execstarts: # execstarts:
# - '/usr/bin/stopcmd' # - '/usr/bin/stopcmd'
# restart_changed: no
# #
# - service_name: ServiceZ # - service_name: ServiceZ
# config_overrides: {} # This is used to add in arbitratry unit file options # config_overrides: {} # This is used to add in arbitratry unit file options

26
handlers/main.yml Normal file
View File

@ -0,0 +1,26 @@
---
# 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.
- name: Restart changed services
systemd:
name: "{{ item.item.service_name | replace(' ', '_') }}"
state: restarted
listen: systemd service changed
with_items: "{{ systemd_services_result.results }}"
when:
- 'item.item.restart_changed | default(systemd_service_restart_changed) | bool'
- 'item.item.state is not defined'
- 'item.item.enabled | default(systemd_service_enabled) | bool'
- 'item | changed'

View File

@ -116,13 +116,21 @@
config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}" config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}"
config_type: "ini" config_type: "ini"
with_items: "{{ systemd_services }}" with_items: "{{ systemd_services }}"
notify:
- systemd service changed
register: systemd_services_result
tags: tags:
- systemd-service - systemd-service
- name: Reload systemd on unit change
systemd:
daemon_reload: yes
when:
- 'systemd_services_result | changed'
- name: Load service - name: Load service
systemd: systemd:
name: "{{ item.service_name | replace(' ', '_') }}" name: "{{ item.service_name | replace(' ', '_') }}"
daemon_reload: yes
enabled: "{{ item.enabled | default(systemd_service_enabled) }}" enabled: "{{ item.enabled | default(systemd_service_enabled) }}"
state: "{{ item.state | default(omit) }}" state: "{{ item.state | default(omit) }}"
with_items: "{{ systemd_services }}" with_items: "{{ systemd_services }}"

View File

@ -25,7 +25,6 @@
- service_name: "test simple service0" - service_name: "test simple service0"
execstarts: "/bin/bash -c 'while true; do sleep 2 && echo test simple service; done'" execstarts: "/bin/bash -c 'while true; do sleep 2 && echo test simple service; done'"
enabled: yes enabled: yes
state: started
- service_name: "test oneshot service0" - service_name: "test oneshot service0"
config_overrides: config_overrides:
Unit: Unit: