Browse Source

added option to create drop-in config

Fabian Peter Hammerle 2 years ago
parent
commit
6902750f2a
2 changed files with 25 additions and 6 deletions
  1. 8 4
      README.md
  2. 17 2
      tasks/main.yml

+ 8 - 4
README.md

@@ -4,15 +4,19 @@
 
 ```yaml
 systemd_unit_name: some.service
-systemd_unit_config: |
-  [Unit]
-  Description=...
-  # ...
 ```
 
 ## Optional Variables
 
 ```yaml
+systemd_unit_config: |
+  [Unit]
+  Description=…
+  # …
+systemd_unit_dropin_config: |
+  [Service]
+  LogLevelMax=notice
+  # …
 systemd_unit_state: started
 systemd_unit_enabled: yes
 systemd_unit_restart_if_active: no

+ 17 - 2
tasks/main.yml

@@ -6,14 +6,27 @@
       {{ systemd_unit_config }}
     mode: a=r
   register: _config_file
+  when: systemd_unit_config is defined
+- name: 'configure systemd unit {{ systemd_unit_name }} via drop-in config'
+  copy:
+    # > Along with a unit file foo.service, a "drop-in" directory foo.service.d/ may
+    # > exist. All files with the suffix ".conf" from this directory will be parsed
+    # > after the unit file itself is parsed.
+    dest: '/etc/systemd/system/{{ systemd_unit_name }}.d/override.conf'
+    content: |
+      # ansible managed
+      {{ systemd_unit_dropin_config }}
+    mode: a=r
+  register: _dropin_config_file
+  when: systemd_unit_dropin_config is defined
 - name: 'fetch state of systemd unit {{ systemd_unit_name }}'
   systemd:
     name: '{{ systemd_unit_name }}'
   register: _unit
-  when: _config_file.changed or systemd_unit_restart_if_active
+  when: _config_file.changed or _dropin_config_file.changed or systemd_unit_restart_if_active
 - name: 'set state of systemd unit {{ systemd_unit_name }}'
   systemd:
-    daemon_reload: '{{ _config_file.changed }}'
+    daemon_reload: '{{ _config_file.changed or _dropin_config_file.changed }}'
     name: '{{ systemd_unit_name }}'
     enabled: '{{ systemd_unit_enabled | default(omit) }}'
     # > `started'/`stopped' are idempotent actions [...]
@@ -22,12 +35,14 @@
       {{ "restarted" if (systemd_unit_state is not defined
                             or systemd_unit_state != "stopped")
                          and (_config_file.changed
+                            or _dropin_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) }}
   when: >-
     _config_file.changed
+    or _dropin_config_file.changed
     or systemd_unit_enabled is defined
     or systemd_unit_state is defined
     or (systemd_unit_restart_if_active