From 6bebc97d86cfa911d4a58d2f9e033885bdd809ba Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Mon, 7 Dec 2020 16:40:04 +0200 Subject: [PATCH] Add option to create systemd native service overrides Since there might be a necessity to override already existing in the system services, like provided ones by system packages, which we just want to adjust, we need option to create service overrides in systemd.servce.d directory. Change-Id: Ic7488edbc0487fe932c706dc26f2f8adb36cb427 --- defaults/main.yml | 14 ++++++++++ ...ice_native_overrides-66f3d0fe3c6d3d1a.yaml | 9 +++++++ tasks/main.yml | 27 +++++++++++++++++++ templates/systemd-service-overrides.j2 | 13 +++++++++ 4 files changed, 63 insertions(+) create mode 100644 releasenotes/notes/systemd_service_native_overrides-66f3d0fe3c6d3d1a.yaml create mode 100644 templates/systemd-service-overrides.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 93a1caa..a7f2c75 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -110,6 +110,10 @@ systemd_environment: {} # `environment_file` -- (optional) set additional environment settings through a given file. this option is a string. # `dynamic_user` -- (optional) Dynamically set a UNIX user and group when the unit is started; only works if systemd >= 235. # `state_directory` -- (optional) Relative path the state directory; only works if systemd >= 235. +# `systemd_overrides` -- (optional) Dictionary that may contain custom structure in config_overrides format that +# will be used to generate systemd overrides in /etc/systemd/system/service_name.service.d/overrides.conf +# `systemd_overrides_only` -- (optional) Boolean variable, when True no service_name.service will be generated and role +# will place only overrides for the existing service. # Under the service dictionary the "sockets" key can be added, which may contain list of the sockets # for the given service_name. Each socket in the lsit may have following structure: @@ -215,4 +219,14 @@ systemd_environment: {} # state: "started" # cron_minute: 30 # cron_hour: 1 +# +# - service_name: ExistingService +# config_overrides: {} +# systemd_overrides_only: True +# systemd_overrides: # This is used for adjusting existing services with arbitratry options +# Unit: +# Wants: +# - ServiceZ +# - ServiceY +# restart_changed: no systemd_services: [] diff --git a/releasenotes/notes/systemd_service_native_overrides-66f3d0fe3c6d3d1a.yaml b/releasenotes/notes/systemd_service_native_overrides-66f3d0fe3c6d3d1a.yaml new file mode 100644 index 0000000..689d9c1 --- /dev/null +++ b/releasenotes/notes/systemd_service_native_overrides-66f3d0fe3c6d3d1a.yaml @@ -0,0 +1,9 @@ +--- +features: + - | + Added `systemd_overrides` and `systemd_overrides_only` keys to the + `systemd_services` dictionary. With help of the `systemd_overrides` + you can define systemd native overrides, which will be placed in + /etc/systemd/system/service_name.service.d/overrides. + `systemd_overrides_only` shows that no service_name.service should not + be created and create only overrides. diff --git a/tasks/main.yml b/tasks/main.yml index c262fd2..5ad7dc1 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -64,6 +64,18 @@ tags: - systemd-service +- name: Create service.d overrides dir + file: + path: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d" + state: directory + owner: "{{ item.systemd_user_name | default(systemd_user_name) }}" + group: "{{ item.systemd_group_name | default(systemd_group_name) }}" + mode: "02755" + when: "'systemd_overrides' in item" + with_items: "{{ systemd_services }}" + tags: + - systemd-service + - name: Create tmpfiles.d entry template: src: "systemd-tmpfiles.j2" @@ -85,12 +97,27 @@ config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}" config_type: "ini" with_items: "{{ systemd_services }}" + when: not (item.systemd_overrides_only | default(False) | bool) notify: - systemd service changed register: systemd_services_result tags: - systemd-service +- name: Place the systemd override + template: + src: "systemd-service-overrides.j2" + dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d/override.conf" + mode: "0644" + owner: "root" + group: "root" + with_items: "{{ systemd_services }}" + when: "'systemd_overrides' in item" + notify: + - systemd service changed + tags: + - systemd-service + - name: Place the systemd timer template: src: "systemd-timer.j2" diff --git a/templates/systemd-service-overrides.j2 b/templates/systemd-service-overrides.j2 new file mode 100644 index 0000000..7a872ae --- /dev/null +++ b/templates/systemd-service-overrides.j2 @@ -0,0 +1,13 @@ +{% for section, params in item.systemd_overrides.items() %} +[{{ section }}] +{% for key, value in params.items() %} +{% if value is iterable and (var is not string and var is not mapping) %} +{% for i in value %} +{{ key }} = {{ i }} +{% endfor %} +{% else %} +{{ key }} = {{ value }} +{% endif %} +{% endfor %} + +{% endfor %}