diff --git a/roles/keepalived/defaults/main.yaml b/roles/keepalived/defaults/main.yaml new file mode 100644 index 0000000..70f9283 --- /dev/null +++ b/roles/keepalived/defaults/main.yaml @@ -0,0 +1,14 @@ +--- +keepalived_package_name: keepalived +keepalived_package_state: present + +keepalived_service_name: keepalived +keepalived_service_state: started +keepalived_service_enabled: true + +keepalived_etc_path: /etc/keepalived + +keepalived_config_path: "{{ keepalived_etc_path }}/keepalived.conf" +keepalived_config_owner: root +keepalived_config_group: root +keepalived_config_mode: "0600" diff --git a/roles/keepalived/handlers/main.yaml b/roles/keepalived/handlers/main.yaml new file mode 100644 index 0000000..96fec88 --- /dev/null +++ b/roles/keepalived/handlers/main.yaml @@ -0,0 +1,12 @@ +--- +- name: reload keepalived + service: + name: "{{ keepalived_service_name }}" + state: reloaded + when: keepalived_service_enabled + +- name: restart keepalived + service: + name: "{{ keepalived_service_name }}" + state: restarted + when: keepalived_service_enabled diff --git a/roles/keepalived/tasks/main.yaml b/roles/keepalived/tasks/main.yaml new file mode 100644 index 0000000..2674207 --- /dev/null +++ b/roles/keepalived/tasks/main.yaml @@ -0,0 +1,20 @@ +--- +- name: install + package: + name: "{{ keepalived_package_name }}" + state: "{{ keepalived_package_state }}" + +- name: configure + template: + src: keepalived.conf.j2 + dest: "{{ keepalived_config_path }}" + owner: "{{ keepalived_config_owner }}" + group: "{{ keepalived_config_group }}" + mode: "{{ keepalived_config_mode }}" + notify: reload keepalived + +- name: service + service: + name: "{{ keepalived_service_name }}" + state: "{{ keepalived_service_state }}" + enabled: "{{ keepalived_service_enabled }}" diff --git a/roles/keepalived/templates/keepalived.conf.j2 b/roles/keepalived/templates/keepalived.conf.j2 new file mode 100644 index 0000000..af52518 --- /dev/null +++ b/roles/keepalived/templates/keepalived.conf.j2 @@ -0,0 +1,78 @@ +{{ ansible_managed | comment }} + +{% if keepalived_global_defs is defined %} +global_defs { +{% for k in keepalived_global_defs %} +{{ k }} {{ v }} +{% endfor %} +} +{% endif %} + +{% if keepalived_vrrp_scripts is defined %} +{% for name, conf in keepalived_vrrp_scripts.items() %} +vrrp_script {{ name }} { +{% if conf.script is defined %} + script "{{ conf.script }}" +{% endif %} +{% if conf.interval is defined %} + interval {{ conf.interval | default(1) }} +{% endif %} +{% if conf.weight is defined %} + weight {{ conf.weight }} +{% endif %} +} +{% endfor %} +{% endif %} + +{% if keepalived_vrrp_instances is defined %} +{% for name, conf in keepalived_vrrp_instances.items() %} +vrrp_instance {{ name }} { +{% if conf.state is defined %} + state {{ conf.state | default("MASTER") }} +{% endif %} +{% if conf.interface is defined %} + interface {{ conf.interface | default("eth0") }} +{% endif %} +{% if conf.virtual_router_id is defined %} + virtual_router_id {{ conf.virtual_router_id }} +{% endif %} +{% if conf.priority is defined %} + priority {{ conf.priority }} +{% endif %} +{% if conf.advert_int is defined %} + advert_int {{ conf.advert_int }} +{% endif %} +{% if conf.authentication is defined %} + authentication { +{% if conf.authentication.auth_type is defined %} + auth_type {{ conf.authentication.auth_type }} +{% endif %} +{% if conf.authentication.auth_pass is defined %} + auth_pass {{ conf.authentication.auth_pass }} +{% endif %} + } +{% if conf.unicast_peer is defined %} + unicast_peer { +{% for x in conf.unicast_peer %} + {{ x }} +{% endfor %} + } +{% endif %} +{% if conf.virtual_ipaddress is defined %} + virtual_ipaddress { +{% for x in conf.virtual_ipaddress %} + {{ x }} +{% endfor %} + } +{% endif %} +{% if conf.track_script is defined %} + track_script { +{% for x in conf.track_script %} + {{ x }} +{% endfor %} + } +{% endif %} +{% endif %} +} +{% endfor %} +{% endif %}