From 287264d1eefb311b472a5f0431ae012cf2033a26 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 23 Aug 2023 17:46:28 +0200 Subject: replace systemd_requires_mounts_for with systemd_depend_mount It should be better and more comprehensible. --- roles/systemd_depend_mount/defaults/main.yml | 2 ++ roles/systemd_depend_mount/handlers/main.yml | 13 ++++++++++ roles/systemd_depend_mount/tasks/main.yml | 28 +++++++++++++++++++++ .../templates/override.conf.j2 | 4 +++ roles/systemd_edit/README.md | 3 --- roles/systemd_edit/defaults/main.yml | 4 --- roles/systemd_edit/tasks/main.yml | 29 ---------------------- roles/systemd_requires_mounts_for/README.md | 2 -- .../systemd_requires_mounts_for/defaults/main.yml | 1 - roles/systemd_requires_mounts_for/tasks/main.yml | 7 ------ 10 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 roles/systemd_depend_mount/defaults/main.yml create mode 100644 roles/systemd_depend_mount/handlers/main.yml create mode 100644 roles/systemd_depend_mount/tasks/main.yml create mode 100644 roles/systemd_depend_mount/templates/override.conf.j2 delete mode 100644 roles/systemd_edit/README.md delete mode 100644 roles/systemd_edit/defaults/main.yml delete mode 100644 roles/systemd_edit/tasks/main.yml delete mode 100644 roles/systemd_requires_mounts_for/README.md delete mode 100644 roles/systemd_requires_mounts_for/defaults/main.yml delete mode 100644 roles/systemd_requires_mounts_for/tasks/main.yml (limited to 'roles') diff --git a/roles/systemd_depend_mount/defaults/main.yml b/roles/systemd_depend_mount/defaults/main.yml new file mode 100644 index 0000000..4d1b819 --- /dev/null +++ b/roles/systemd_depend_mount/defaults/main.yml @@ -0,0 +1,2 @@ +#systemd_depend_mount_dir: +systemd_depend_mount_services: [] diff --git a/roles/systemd_depend_mount/handlers/main.yml b/roles/systemd_depend_mount/handlers/main.yml new file mode 100644 index 0000000..81bae32 --- /dev/null +++ b/roles/systemd_depend_mount/handlers/main.yml @@ -0,0 +1,13 @@ +- name: Reload systemd daemon + become: true + ansible.builtin.systemd_service: + daemon_reload: true + listen: systemd_depend_mount_restart + +- name: Restart systemd services + become: true + ansible.builtin.systemd_service: + name: '{{ item }}' + state: restarted + listen: systemd_depend_mount_restart + loop: "{{ systemd_depend_mount_result.results | selectattr('changed', 'equalto', true) | map(attribute='service') | list }}" diff --git a/roles/systemd_depend_mount/tasks/main.yml b/roles/systemd_depend_mount/tasks/main.yml new file mode 100644 index 0000000..d759b00 --- /dev/null +++ b/roles/systemd_depend_mount/tasks/main.yml @@ -0,0 +1,28 @@ +- name: Create override directory + become: true + ansible.builtin.file: + path: '/etc/systemd/system/{{ service }}.service.d' + state: directory + owner: root + group: root + mode: '755' + loop: '{{ systemd_depend_mount_services }}' + loop_control: + loop_var: service + +- name: Create service override + become: true + ansible.builtin.template: + src: override.conf.j2 + dest: '/etc/systemd/system/{{ service }}.service.d/depend_mount_{{ systemd_depend_mount_dir | regex_replace("/", "_") }}.conf' + owner: root + group: root + mode: '644' + register: systemd_depend_mount_result + notify: systemd_depend_mount_restart + loop: '{{ systemd_depend_mount_services }}' + loop_control: + loop_var: service + +- name: Restart systemd services if necessary + ansible.builtin.meta: flush_handlers diff --git a/roles/systemd_depend_mount/templates/override.conf.j2 b/roles/systemd_depend_mount/templates/override.conf.j2 new file mode 100644 index 0000000..797cd67 --- /dev/null +++ b/roles/systemd_depend_mount/templates/override.conf.j2 @@ -0,0 +1,4 @@ +{{ ansible_managed | comment }} + +[Unit] +RequiresMountsFor={{ systemd_depend_mount_dir }} diff --git a/roles/systemd_edit/README.md b/roles/systemd_edit/README.md deleted file mode 100644 index 23af7c8..0000000 --- a/roles/systemd_edit/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This role is similar to running `systemctl edit SERVICE`; it creates an -override.conf file for the service in /etc/systemd and sets the required -values there. diff --git a/roles/systemd_edit/defaults/main.yml b/roles/systemd_edit/defaults/main.yml deleted file mode 100644 index ced03e5..0000000 --- a/roles/systemd_edit/defaults/main.yml +++ /dev/null @@ -1,4 +0,0 @@ -#service_name: -#service_section: -#service_option: -#service_values: diff --git a/roles/systemd_edit/tasks/main.yml b/roles/systemd_edit/tasks/main.yml deleted file mode 100644 index 98dbd27..0000000 --- a/roles/systemd_edit/tasks/main.yml +++ /dev/null @@ -1,29 +0,0 @@ -- name: Edit systemd service - become: true - block: - - name: 'Create override directory: {{ service_name }}' - ansible.builtin.file: - path: '/etc/systemd/system/{{ service_name }}.service.d' - state: directory - owner: root - group: root - mode: '755' - - - name: 'Set up override.conf: {{ service_name }}' - community.general.ini_file: - dest: '/etc/systemd/system/{{ service_name }}.service.d/override.conf' - section: '{{ service_section }}' - option: '{{ service_option }}' - values: '{{ service_values }}' - no_extra_spaces: true - owner: root - group: root - mode: '644' - register: overridden - - - name: 'Restart service: {{ service_name }}' - ansible.builtin.systemd_service: - daemon_reload: true - name: '{{ service_name }}' - state: restarted - when: overridden.changed diff --git a/roles/systemd_requires_mounts_for/README.md b/roles/systemd_requires_mounts_for/README.md deleted file mode 100644 index 7aef90f..0000000 --- a/roles/systemd_requires_mounts_for/README.md +++ /dev/null @@ -1,2 +0,0 @@ -This role modifies a systemd service to require a mounted directory before -starting it. diff --git a/roles/systemd_requires_mounts_for/defaults/main.yml b/roles/systemd_requires_mounts_for/defaults/main.yml deleted file mode 100644 index 6ae56f1..0000000 --- a/roles/systemd_requires_mounts_for/defaults/main.yml +++ /dev/null @@ -1 +0,0 @@ -#mount_dir: diff --git a/roles/systemd_requires_mounts_for/tasks/main.yml b/roles/systemd_requires_mounts_for/tasks/main.yml deleted file mode 100644 index 4459d59..0000000 --- a/roles/systemd_requires_mounts_for/tasks/main.yml +++ /dev/null @@ -1,7 +0,0 @@ -- name: Edit systemd service - ansible.builtin.include_role: - name: systemd_edit - vars: - service_section: Unit - service_option: RequiresMountsFor - service_values: ['{{ mount_dir }}'] -- cgit v1.2.3