ansible-role-systemd_service/tasks/main.yml
Dmitriy Rabotyagov a0fb5a6df2 Filter loop lists instead of conditionally run them
Filtering loops should execute faster and do not iterate over elements
that we don't need.

Change-Id: Iee2e706c50b59679f48afbd6f067922887154e5c
2023-10-27 22:25:47 +02:00

204 lines
7.2 KiB
YAML

---
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Create TEMP run dir
file:
path: "{{ item.systemd_run_dir | default(systemd_run_dir) }}/{{ item.systemd_slice_name | default(systemd_slice_name) | replace(' ', '_') }}"
state: directory
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
mode: "02755"
loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
path: "{{ item.systemd_run_dir | default(systemd_run_dir) }}/{{ item.systemd_slice_name | default(systemd_slice_name) | replace(' ', '_') }}"
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
tags:
- systemd-service
- name: Create TEMP service lock dir
file:
path: "{{ item.systemd_lock_dir | default(systemd_lock_dir) }}/{{ item.systemd_slice_name | default(systemd_slice_name) | replace(' ', '_') }}"
state: directory
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
mode: "02755"
loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
path: "{{ item.systemd_lock_dir | default(systemd_lock_dir) }}/{{ item.systemd_slice_name | default(systemd_slice_name) | replace(' ', '_') }}"
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
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"
loop: "{{ systemd_services | rejectattr('systemd_overrides', 'undefined') }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
path: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d"
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
tags:
- systemd-service
- name: Create tmpfiles.d entry
template:
src: "systemd-tmpfiles.j2"
dest: "/etc/tmpfiles.d/{{ item.systemd_tempd_prefix | default(systemd_tempd_prefix) }}-{{ item.service_name | replace(' ', '_') }}.conf"
loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-tmpfiles.j2"
dest: "/etc/tmpfiles.d/{{ item.systemd_tempd_prefix | default(systemd_tempd_prefix) }}-{{ item.service_name | replace(' ', '_') }}.conf"
tags:
- systemd-service
- name: Place the systemd service
openstack.config_template.config_template:
src: "systemd-service.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service"
config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}"
config_type: "ini"
loop: "{{ systemd_services | rejectattr('systemd_overrides_only', 'true') }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-service.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service"
config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}"
config_type: "ini"
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"
loop: "{{ systemd_services | rejectattr('systemd_overrides', 'undefined') }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-service-overrides.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d/override.conf"
notify:
- systemd service changed
register: systemd_override_result
tags:
- systemd-service
- name: Place the systemd timer
template:
src: "systemd-timer.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.timer"
mode: "0644"
owner: "root"
group: "root"
loop: "{{ systemd_services | selectattr('timer', 'defined') }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-timer.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.timer"
notify:
- systemd service changed
register: systemd_timer_result
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) }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-socket.j2"
dest: "/etc/systemd/system/{{ item.1.socket_name | replace(' ', '_') }}.socket"
notify:
- systemd socket changed
- 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) or
(systemd_override_result is changed ) or
(systemd_socket.results | selectattr('changed', 'true') | length > 0)
- name: Including systemd_load tasks
include_tasks: systemd_load.yml
vars:
_systemd_service_name: "{{ service.service_name | replace(' ', '_') }}"
loop_label:
service_name: "{{ _systemd_service_name }}"
loop: "{{ systemd_services }}"
loop_control:
loop_var: service
label: "{{ loop_label | to_json }}"
args:
apply:
tags:
- systemd-service
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) }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
name: "{{ item.socket_name | replace(' ', '_') }}.socket"
enabled: "{{ item.enabled | default(systemd_service_enabled) }}"
state: "{{ item.state | default(omit) }}"
tags:
- systemd-service