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
This commit is contained in:
Dmitriy Rabotyagov 2020-12-07 16:40:04 +02:00
parent 763eb7638f
commit 6bebc97d86
4 changed files with 63 additions and 0 deletions

View File

@ -110,6 +110,10 @@ systemd_environment: {}
# `environment_file` -- (optional) set additional environment settings through a given file. this option is a string. # `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. # `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. # `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 # 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: # for the given service_name. Each socket in the lsit may have following structure:
@ -215,4 +219,14 @@ systemd_environment: {}
# state: "started" # state: "started"
# cron_minute: 30 # cron_minute: 30
# cron_hour: 1 # 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: [] systemd_services: []

View File

@ -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.

View File

@ -64,6 +64,18 @@
tags: tags:
- systemd-service - 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 - name: Create tmpfiles.d entry
template: template:
src: "systemd-tmpfiles.j2" src: "systemd-tmpfiles.j2"
@ -85,12 +97,27 @@
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 }}"
when: not (item.systemd_overrides_only | default(False) | bool)
notify: notify:
- systemd service changed - systemd service changed
register: systemd_services_result register: systemd_services_result
tags: tags:
- systemd-service - 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 - name: Place the systemd timer
template: template:
src: "systemd-timer.j2" src: "systemd-timer.j2"

View File

@ -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 %}