keepalived: better support for notify scripts

This commit is contained in:
jeltz 2022-08-27 12:55:53 +02:00
parent c6ac61aa53
commit 9820ae62e8
Signed by: jeltz
GPG key ID: 800882B66C0C3326
4 changed files with 61 additions and 12 deletions

View file

@ -1,3 +1,6 @@
--- ---
keepalived__virtual_addresses: {} keepalived__virtual_addresses: {}
keepalived__notify_master: []
keepalived__notify_backup: []
keepalived__notify_fault: []
... ...

View file

@ -5,11 +5,18 @@
- name: Configure keepalived - name: Configure keepalived
template: template:
src: keepalived.conf.j2 src: "{{ item.src }}"
dest: /etc/keepalived/keepalived.conf dest: "{{ item.dest }}"
owner: root owner: root
group: root group: root
mode: u=rw,g=,o= mode: "{{ item.mode }}"
loop:
- src: keepalived.conf.j2
dest: /etc/keepalived/keepalived.conf
mode: u=rw,g=,o=
- src: notify.sh.j2
dest: /etc/keepalived/notify.sh
mode: u=rwx,g=,o=
notify: notify:
- Reload keepalived - Reload keepalived

View file

@ -20,6 +20,7 @@ set ipv6_enabled =
| flatten | ansible.utils.ipv6) | flatten | ansible.utils.ipv6)
%} %}
{% if ipv4_enabled and ipv6_enabled %}
vrrp_sync_group group { vrrp_sync_group group {
group { group {
{% if ipv4_enabled %} {% if ipv4_enabled %}
@ -29,16 +30,11 @@ vrrp_sync_group group {
instance_v6 instance_v6
{% endif %} {% endif %}
} }
{% if keepalived__notify_master is defined %} notify_master "/etc/keepalived/notify.sh master"
notify_master {{ keepalived__notify_master | enquote('"') }} notify_backup "/etc/keepalived/notify.sh backup"
{% endif %} notify_fault "/etc/keepalived/notify.sh fault"
{% if keepalived__notify_backup is defined %}
notify_backup {{ keepalived__notify_backup | enquote('"') }}
{% endif %}
{% if keepalived__notify_fault is defined %}
notify_fault {{ keepalived__notify_fault | enquote('"') }}
{% endif %}
} }
{% endif %}
{% if ipv4_enabled %} {% if ipv4_enabled %}
vrrp_instance instance_v4 { vrrp_instance instance_v4 {
@ -58,6 +54,11 @@ vrrp_instance instance_v4 {
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
} }
{% if not (ipv4_enabled and ipv6_enabled) %}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
{% endif %}
} }
{% endif %} {% endif %}
@ -79,5 +80,10 @@ vrrp_instance instance_v6 {
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
} }
{% if not (ipv4_enabled and ipv6_enabled) %}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
{% endif %}
} }
{% endif %} {% endif %}

View file

@ -0,0 +1,33 @@
#!/bin/bash
master=(
{% for notify in keepalived__notify_master %}
{{ notify | quote }}
{% endfor %}
)
backup=(
{% for notify in keepalived__notify_backup %}
{{ notify | quote }}
{% endfor %}
)
fault=(
{% for notify in keepalived__notify_fault %}
{{ notify | quote }}
{% endfor %}
)
case "$1" in
master | backup | fault)
scripts="$1[@]"
;;
*)
echo "Usage: $0 (master|backup|fault)" >&2
exit 1
esac
for script in "${!scripts}"
do
eval "${script}"
done