From 72efb59ad531f4f2571bc340eeb2f2eb6ca5ebbd Mon Sep 17 00:00:00 2001 From: Logan V Date: Tue, 20 Mar 2018 22:56:49 -0500 Subject: [PATCH] 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 --- defaults/main.yml | 4 ++++ handlers/main.yml | 26 ++++++++++++++++++++++++++ tasks/main.yml | 10 +++++++++- tests/test.yml | 1 - 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 handlers/main.yml diff --git a/defaults/main.yml b/defaults/main.yml index 03b78bc..43634f2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -17,6 +17,9 @@ systemd_user_name: root systemd_group_name: root 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. systemd_tempd_prefix: tempd @@ -88,6 +91,7 @@ systemd_default_service_type: simple # - '/usr/bin/ServiceY' # execstarts: # - '/usr/bin/stopcmd' +# restart_changed: no # # - service_name: ServiceZ # config_overrides: {} # This is used to add in arbitratry unit file options diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..7a18508 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,26 @@ +--- +# Copyright 2018, Logan Vig +# +# 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' \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index ab28b42..8ad4941 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -116,13 +116,21 @@ config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}" config_type: "ini" with_items: "{{ systemd_services }}" + notify: + - systemd service changed + register: systemd_services_result tags: - systemd-service +- name: Reload systemd on unit change + systemd: + daemon_reload: yes + when: + - 'systemd_services_result | changed' + - name: Load service systemd: name: "{{ item.service_name | replace(' ', '_') }}" - daemon_reload: yes enabled: "{{ item.enabled | default(systemd_service_enabled) }}" state: "{{ item.state | default(omit) }}" with_items: "{{ systemd_services }}" diff --git a/tests/test.yml b/tests/test.yml index acdff39..f5d6742 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -25,7 +25,6 @@ - service_name: "test simple service0" execstarts: "/bin/bash -c 'while true; do sleep 2 && echo test simple service; done'" enabled: yes - state: started - service_name: "test oneshot service0" config_overrides: Unit: