From 98722e9afce34743e3d5b217af5bc67dbc93691b Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <ynerant@crans.org>
Date: Sun, 11 Jul 2021 01:17:01 +0200
Subject: [PATCH] [keepalived] Override StopPost in systemd unit of keepalived

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
---
 roles/keepalived/tasks/main.yml               | 19 ++++++++++++++++++-
 .../keepalived.service.d/override.conf.j2     |  4 ++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 roles/keepalived/templates/systemd/system/keepalived.service.d/override.conf.j2

diff --git a/roles/keepalived/tasks/main.yml b/roles/keepalived/tasks/main.yml
index 87362375..a7d77af9 100644
--- a/roles/keepalived/tasks/main.yml
+++ b/roles/keepalived/tasks/main.yml
@@ -11,7 +11,7 @@
 - name: Use buster-backports to install keepalived
   template:
     src: apt/preferences.d/keepalived.j2
-    dest: apt/preferences.d/keepalived
+    dest: /etc/apt/preferences.d/keepalived
     owner: root
     group: root
     mode: 0644
@@ -35,8 +35,25 @@
     mode: 0644
   notify: Reload keepalived.service
 
+- name: Create keepalived service directory for systemd
+  file:
+    path: /etc/systemd/system/keepalived.service.d
+    owner: root
+    group: root
+    mode: 0755
+    state: directory
+
+- name: Override keepalived StopPost to stop some services after keepalived switch
+  template:
+    src: systemd/system/keepalived.service.d/override.conf.j2
+    dest: /etc/systemd/system/keepalived.service.d/override.conf
+    owner: root
+    group: root
+    mode: 0644
+
 - name: Start and enable keepalived
   service:
     name: keepalived
+    daemon-reload: true
     state: started
     enabled: yes
diff --git a/roles/keepalived/templates/systemd/system/keepalived.service.d/override.conf.j2 b/roles/keepalived/templates/systemd/system/keepalived.service.d/override.conf.j2
new file mode 100644
index 00000000..9fa1e5df
--- /dev/null
+++ b/roles/keepalived/templates/systemd/system/keepalived.service.d/override.conf.j2
@@ -0,0 +1,4 @@
+{{ ansible_header | comment }}
+
+[Service]
+ExecStopPost=/bin/sh -c "{% for instance in keepalived.instances %}/var/local/services/keepalived/keepalived.py INSTANCE {{ instance.name }} STOP {{ instance.priority }}; {% endfor %}"
-- 
GitLab