Add possibility to configure systemd sockets

We need to be able to configure systemd-sockets. Since they are attached
to the service it make sense to make it as an element to the service.

Change-Id: Ic481921151fc8d7af7f1ca9b003adb8e3c967e16
This commit is contained in:
Dmitriy Rabotyagov 2020-11-18 16:39:54 +02:00
parent 11af14d126
commit 763eb7638f
4 changed files with 73 additions and 1 deletions

View File

@ -111,6 +111,16 @@ systemd_environment: {}
# `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.
# 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:
# `socket_name` -- (required) Name of created socket
# `after_targets` -- (optional) Start the socket after this list of dependency units.
# `before_targets` -- (optional) Start the socket before this list of dependency units.
# `bind_targets` -- (optional) Bind the socket to this dependency unit.
# `enabled` -- (optional) Set the enabled state of the socket.
# `options` -- (optional) Additional options, like `ListenStream` or other
# `state` -- (optional) Set the running state of the socket.
# Under the service dictionary the "timer" key can be added which will enable a given service
# as a timer (Legacy cron job).
# `options` -- (optional) This allows any section or key=value pair to be set within the systemd timer file.
@ -170,6 +180,19 @@ systemd_environment: {}
# execstops
# - /usr/bin/stopcmd1
# - /usr/bin/stopcmd2
# sockets:
# - socket_name: SocketServiceZ
# after_targets:
# - ServiceZ.socket
# before_targets:
# - ServiceY.service
# bind_targets:
# - ServiceZ.socket
# enabled: true
# state: started
# options:
# SocketMode: 0600
# ListenStream: /var/run/ServiceZ
#
# - service_name: TimerServiceW
# config_overrides: {} # This is used to add in arbitratry unit file options

View File

@ -0,0 +1,4 @@
---
features:
- |
Added sockets key to configure systemd-sockets for the systemd service.

View File

@ -107,11 +107,25 @@
tags:
- systemd-service
- name: Place the systemd socket
template:
src: "systemd-socket.j2"
dest: "/etc/systemd/system/{{ item.1.socket_name | replace(' ', '_') }}.socket"
mode: "0644"
owner: "root"
group: "root"
loop: "{{ systemd_services | subelements('sockets', skip_missing=True) }}"
notify:
- systemd service changed
register: systemd_socket
tags:
- systemd-service
- name: Reload systemd on unit change
systemd:
daemon_reload: yes
when:
- (systemd_services_result is changed) or (systemd_timer_result is changed)
- (systemd_services_result is changed) or (systemd_timer_result is changed) or ('true' in systemd_socket.results | map(attribute='changed') | list )
- name: Load service
systemd:
@ -132,3 +146,12 @@
with_items: "{{ systemd_services }}"
tags:
- systemd-service
- name: Load socket
systemd:
name: "{{ item.socket_name | replace(' ', '_') }}.socket"
enabled: "{{ item.enabled | default(systemd_service_enabled) }}"
state: "{{ item.state | default(omit) }}"
loop: "{{ systemd_services | selectattr('sockets', 'defined') | map(attribute='sockets') | flatten(1) }}"
tags:
- systemd-service

View File

@ -0,0 +1,22 @@
# {{ ansible_managed }}
[Unit]
Description={{ item.1.socket_name }} socket
{% for target in item.1.after_targets | default([]) %}
After={{ target }}
{% endfor %}
{% for target in item.1.before_targets | default([]) %}
Before={{ target }}
{% endfor %}
{% for target in item.1.bind_targets | default([]) %}
BindsTo={{ target }}
{% endfor %}
[Socket]
Service={{ item.0.service_name | replace(' ', '_') }}.service
{% for key, var in item.1.options.items() | default({}) %}
{{ key }}={{ var }}
{% endfor %}
[Install]
WantedBy=sockets.target