diff --git a/defaults/main.yml b/defaults/main.yml index 86555cf..93a1caa 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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 diff --git a/releasenotes/notes/systemd_socket-288b17560a578814.yaml b/releasenotes/notes/systemd_socket-288b17560a578814.yaml new file mode 100644 index 0000000..8aa311f --- /dev/null +++ b/releasenotes/notes/systemd_socket-288b17560a578814.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Added sockets key to configure systemd-sockets for the systemd service. diff --git a/tasks/main.yml b/tasks/main.yml index cf01215..c262fd2 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 diff --git a/templates/systemd-socket.j2 b/templates/systemd-socket.j2 new file mode 100644 index 0000000..a3206e2 --- /dev/null +++ b/templates/systemd-socket.j2 @@ -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