From f77744dca8ea179929b0a9e4f5f68edf0c47d1e3 Mon Sep 17 00:00:00 2001 From: ace Date: Wed, 8 Feb 2023 01:42:20 +0300 Subject: [PATCH] initial commit --- README.md | 28 +++++++++++++++ defaults/main.yml | 3 ++ handlers/main.yaml | 8 +++++ meta/main.yml | 1 + tasks/main.yml | 35 ++++++++++++++++++ templates/keepalived.conf.j2 | 70 ++++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yaml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/keepalived.conf.j2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..f1dc7de --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +Setup keepalived service + +Config example: + + keepalived_config: + global_defs: + router_id: t1-dev-rabbitmq + vrrp_track_process: + haproxy: + process: haproxy + quorum: 1 + delay: 2 + vrrp_instance: + VRRP_1: + state: MASTER + virtual_router_id: 50 + priority: "{{ ansible_default_ipv4.address.split('.')[-1][-2:] }}" + interface: "{{ ansible_default_ipv4.interface }}" + unicast_src_ip: "{{ ansible_default_ipv4.address }}" + unicast_peer: + - 10.89.10.71 + - 10.89.10.72 + - 10.89.10.73 + virtual_ipaddress: + - "10.89.10.112/32 dev {{ ansible_default_ipv4.interface }}" + track_process: + - haproxy + diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..282540d --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +keepalived_config_override: [] +keepalived_default_config: [] +keepalived_script_user: root diff --git a/handlers/main.yaml b/handlers/main.yaml new file mode 100644 index 0000000..80e7ebd --- /dev/null +++ b/handlers/main.yaml @@ -0,0 +1,8 @@ +--- +- name: Restart keepalived + throttle: 1 + systemd: + name: keepalived + state: restarted + daemon_reload: yes + when: "not keepalived_setup.changed or not keepalived_enable_and_start.changed" diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..32cf5dd --- /dev/null +++ b/meta/main.yml @@ -0,0 +1 @@ +dependencies: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..ac30bed --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,35 @@ +--- +- name: Install keepalived + package: + name: keepalived + state: present + register: keepalived_setup + +- block: + - name: Merge config for keepalived + set_fact: + keepalived_config: "{{ keepalived_default_config | combine(keepalived_config, recursive=true) }}" + + - name: Add keepalived config + template: + src: "keepalived.conf.j2" + dest: "/etc/keepalived/keepalived.conf" + notify: + - Restart keepalived + when: not keepalived_config_override + +- name: Override keepalived config + copy: + content: "{{ keepalived_config_override }}" + dest: "/etc/keepalived/keepalived.conf" + notify: + - Restart keepalived + when: keepalived_config_override + +- name: Enable and start keepalived service + systemd: + name: keepalived + state: started + enabled: yes + daemon_reload: yes + register: keepalived_enable_and_start diff --git a/templates/keepalived.conf.j2 b/templates/keepalived.conf.j2 new file mode 100644 index 0000000..b24f416 --- /dev/null +++ b/templates/keepalived.conf.j2 @@ -0,0 +1,70 @@ +global_defs { +{% if keepalived_config.vrrp_script is defined %} + enable_script_security + script_user {{ keepalived_script_user }} +{% endif %} +{% for key, value in keepalived_config.global_defs.items() %} +{% if value | type_debug != 'list' %} + {{key}} {{value}} +{% else %} +{% for list_value in value %} + {{key}} {{ list_value }} +{% endfor %} +{% endif %} +{% endfor %} +} + +{% if keepalived_config.vrrp_script is defined %} +{% for vrrp_script in keepalived_config.vrrp_script %} +vrrp_script {{ vrrp_script }} { +{% for key, value in keepalived_config.vrrp_script[vrrp_script].items() %} +{% if value | type_debug != 'list' %} + {{key}} {{value}} +{% else %} +{% for list_value in value %} + {{key}} {{ list_value }} +{% endfor %} +{% endif %} +{% endfor %} +} +{% endfor %} +{% endif %} + +{% if keepalived_config.vrrp_track_process is defined %} +{% for vrrp_track_process in keepalived_config.vrrp_track_process %} +vrrp_track_process {{ vrrp_track_process }} { +{% for key, value in keepalived_config.vrrp_track_process[vrrp_track_process].items() %} +{% if value | type_debug != 'list' %} + {{key}} {{value}} +{% else %} +{% for list_value in value %} + {{key}} {{ list_value }} +{% endfor %} +{% endif %} +{% endfor %} +} +{% endfor %} +{% endif %} + +{% for vrrp_instance in keepalived_config.vrrp_instance %} +vrrp_instance {{ vrrp_instance }} { +{% for key, value in keepalived_config.vrrp_instance[vrrp_instance].items() %} +{% if value | type_debug != 'list' %} + {{key}} {{value}} +{% else %} + {{ key }} { +{% if key == 'unicast_peer' %} +{% for list_value in value %} + {{ list_value if list_value != ansible_default_ipv4.address }} +{% endfor %} +{% else %} +{% for list_value in value %} + {{ list_value }} +{% endfor %} +{% endif %} + } +{% endif %} +{% endfor %} +} +{% endfor %} +