Browse Source

added option "systemd_unit_restart_if_active"

Fabian Peter Hammerle 2 years ago
parent
commit
1c611b25fd
3 changed files with 10 additions and 2 deletions
  1. 1 0
      README.md
  2. 1 0
      defaults/main.yml
  3. 8 2
      tasks/main.yml

+ 1 - 0
README.md

@@ -15,4 +15,5 @@ systemd_unit_config: |
 ```yaml
 systemd_unit_state: started
 systemd_unit_enabled: yes
+systemd_unit_restart_if_active: no
 ```

+ 1 - 0
defaults/main.yml

@@ -0,0 +1 @@
+systemd_unit_restart_if_active: no

+ 8 - 2
tasks/main.yml

@@ -10,16 +10,19 @@
   systemd:
     name: '{{ systemd_unit_name }}'
   register: _unit
-  when: _config_file.changed
+  when: _config_file.changed or systemd_unit_restart_if_active
 - name: 'set state of systemd unit {{ systemd_unit_name }}'
   systemd:
     daemon_reload: '{{ _config_file.changed }}'
     name: '{{ systemd_unit_name }}'
     enabled: '{{ systemd_unit_enabled | default(omit) }}'
+    # > `started'/`stopped' are idempotent actions [...]
+    # > `restarted' will always bounce the service
     state: >-
       {{ "restarted" if (systemd_unit_state is not defined
                             or systemd_unit_state != "stopped")
-                         and _config_file.changed
+                         and (_config_file.changed
+                            or systemd_unit_restart_if_active)
                          and 'ActiveState' in _unit.status
                          and _unit.status.ActiveState == "active"
          else systemd_unit_state | default(omit) }}
@@ -27,3 +30,6 @@
     _config_file.changed
     or systemd_unit_enabled is defined
     or systemd_unit_state is defined
+    or (systemd_unit_restart_if_active
+        and 'ActiveState' in _unit.status
+        and _unit.status.ActiveState == "active")