From 622921151674a71fda88b405a576847e69cd3c83 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 23 Aug 2023 18:06:40 +0200 Subject: add role systemd_depend_iface --- roles/systemd_depend_iface/defaults/main.yml | 2 ++ roles/systemd_depend_iface/handlers/main.yml | 13 ++++++++++ roles/systemd_depend_iface/tasks/main.yml | 28 ++++++++++++++++++++++ .../templates/override.conf.j2 | 5 ++++ 4 files changed, 48 insertions(+) create mode 100644 roles/systemd_depend_iface/defaults/main.yml create mode 100644 roles/systemd_depend_iface/handlers/main.yml create mode 100644 roles/systemd_depend_iface/tasks/main.yml create mode 100644 roles/systemd_depend_iface/templates/override.conf.j2 diff --git a/roles/systemd_depend_iface/defaults/main.yml b/roles/systemd_depend_iface/defaults/main.yml new file mode 100644 index 0000000..5c6c6a4 --- /dev/null +++ b/roles/systemd_depend_iface/defaults/main.yml @@ -0,0 +1,2 @@ +#systemd_depend_iface_name: +systemd_depend_iface_services: [] diff --git a/roles/systemd_depend_iface/handlers/main.yml b/roles/systemd_depend_iface/handlers/main.yml new file mode 100644 index 0000000..2782200 --- /dev/null +++ b/roles/systemd_depend_iface/handlers/main.yml @@ -0,0 +1,13 @@ +- name: Reload systemd daemon + become: true + ansible.builtin.systemd_service: + daemon_reload: true + listen: systemd_depend_iface_restart + +- name: Restart systemd services + become: true + ansible.builtin.systemd_service: + name: '{{ item }}' + state: restarted + listen: systemd_depend_iface_restart + loop: "{{ systemd_depend_iface_result.results | selectattr('changed', 'equalto', true) | map(attribute='service') | list }}" diff --git a/roles/systemd_depend_iface/tasks/main.yml b/roles/systemd_depend_iface/tasks/main.yml new file mode 100644 index 0000000..1bc8590 --- /dev/null +++ b/roles/systemd_depend_iface/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_iface_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_iface_{{ systemd_depend_iface_name }}.conf' + owner: root + group: root + mode: '644' + register: systemd_depend_iface_result + notify: systemd_depend_iface_restart + loop: '{{ systemd_depend_iface_services }}' + loop_control: + loop_var: service + +- name: Restart systemd services if necessary + ansible.builtin.meta: flush_handlers diff --git a/roles/systemd_depend_iface/templates/override.conf.j2 b/roles/systemd_depend_iface/templates/override.conf.j2 new file mode 100644 index 0000000..55c4f43 --- /dev/null +++ b/roles/systemd_depend_iface/templates/override.conf.j2 @@ -0,0 +1,5 @@ +{{ ansible_managed | comment }} + +[Unit] +BindsTo=sys-devices-virtual-net-{{ systemd_depend_iface_name }}.device +After=sys-devices-virtual-net-{{ systemd_depend_iface_name }}.device -- cgit v1.2.3