diff --git a/defaults/main.yml b/defaults/main.yml index b1aea24..1b1bf86 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -13,7 +13,13 @@ keepalived_ipv6: "" keepalived_peers: [] -keepalived_notify_script_enabled: false +keepalived_notify: {} +# Exemple: +# pre: "" +# master: "" +# backup: "" +# fault: "" +# default: "" keepalived_track_scripts: [] # Exemple: diff --git a/tasks/main.yml b/tasks/main.yml index ecd3baa..8bfd30f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -8,32 +8,33 @@ - name: Déployer la configuration ansible.builtin.template: + src: keepalived.j2 + dest: /etc/keepalived/keepalived.conf owner: root group: root mode: u=rw,g=r,o=r - src: keepalived.j2 - dest: /etc/keepalived/keepalived.conf - become: true - notify: Redémarrer keepalived.service - -- name: Déployer les scripts - ansible.builtin.copy: - src: keepalived/notify.sh - dest: /etc/keepalived/ - owner: keepalived_script - group: keepalived_script - mode: u=rx,g=rx,o= - when: keepalived_notify_script_enabled become: true notify: Redémarrer keepalived.service - name: Déployer le fichier de configuration de sudoers ansible.builtin.copy: - src: keepalived/sudoers - dest: /etc/sudoers.d/keepalived + src: sudoers.j2 + dest: /etc/sudoers.d/keepalived_script owner: root group: root mode: u=rw,g=r,o=r validate: /usr/sbin/visudo -cf %s - when: "'keepalived/sudoers' is file" + when: keepalived_notify | length > 0 become: true + notify: Redémarrer keepalived.service + +- name: Déployer les scripts + ansible.builtin.template: + src: notify.bash.j2 + dest: /etc/keepalived/notify.bash + owner: root + group: keepalived_script + mode: u=rwx,g=rx,o= + when: keepalived_notify | length > 0 + become: true + notify: Redémarrer keepalived.service diff --git a/templates/keepalived.j2 b/templates/keepalived.j2 index 3aab2be..049b3f1 100644 --- a/templates/keepalived.j2 +++ b/templates/keepalived.j2 @@ -64,7 +64,7 @@ vrrp_instance VIP_{{ keepalived_uid }} { } {% endif %} -{% if keepalived_notify_script_enabled %} +{% if keepalived_notify | length > 0 %} notify /etc/keepalived/notify.sh {% endif %} diff --git a/templates/notify.bash.j2 b/templates/notify.bash.j2 new file mode 100644 index 0000000..ffe977d --- /dev/null +++ b/templates/notify.bash.j2 @@ -0,0 +1,38 @@ +#!/bin/bash +# {{ ansible_managed }} + +TYPE=$1 +NAME=$2 +STATE=$3 + +{% if keepalived_notify.pre | length > 0 %} +{{ keepalived_notify.pre }} +{% endif %} + +# Use keepalived state +case $STATE in +{% if keepalived_notify.master | length > 0 %} + "MASTER") + {{ keepalived_notify.master | indent(4) }} + exit 0 + ;; +{% endif %} +{% if keepalived_notify.backup | length > 0 %} + "BACKUP") + {{ keepalived_notify.backup | indent(4) }} + exit 0 + ;; +{% endif %} +{% if keepalived_notify.fault | length > 0 %} + "FAULT") + {{ keepalived_notify.fault | indent(4) }} + exit 0 + ;; +{% endif %} +{% if keepalived_notify.default | length > 0 %} + *) + {{ keepalived_notify.default | indent(4) }} + exit 1 + ;; +{% endif %} +esac