diff --git a/LICENSE b/LICENSE index 137069b..ac2238b 100644 --- a/LICENSE +++ b/LICENSE @@ -58,7 +58,7 @@ APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright [yyyy] [name of copyright owner] +Copyright 2021 Janic Voser Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 7cab261..d574650 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ -# KeepAlived +# Setup Keepalived VIP with Ansible -Manage Keepalived VIPs with Ansible \ No newline at end of file +# Usage + +Every thing you need to setup to configure VIP + +## Prerequirements + +### Same Network +The Virtual IP is taking advantage of the vrrp, so the Servers need to reach each other. + +### SSH-Key +For ansible to work exchange the SSH-Keys from the Ansible Master to the Ansible managed servers. + +## Configuring your Infrastructure +Just adjust the file ./inventory/hosts.ini, with your hosts and variables. + +## Installing the Keepalived service +When you have configured everything, just run the Ansible-playbook[configure.yaml]. +```sh +ansible-playbook -i [Inventory File] configure.yaml +``` + +## Removing the Keepalived service +When you want to remove the Keepalived service just run the Ansible-playbook[remove.yaml]. +```sh +ansible-playbook -i [Inventory File] remove.yaml diff --git a/configure.yaml b/configure.yaml new file mode 100644 index 0000000..445302a --- /dev/null +++ b/configure.yaml @@ -0,0 +1,18 @@ +--- + +- hosts: keepalived + gather_facts: yes + become: yes + roles: + - role: prereq + - role: download + +- hosts: master + become: yes + roles: + - role: keepalived/master + +- hosts: backup + become: yes + roles: + - role: keepalived/backup \ No newline at end of file diff --git a/inventory/hosts.ini b/inventory/hosts.ini new file mode 100644 index 0000000..a2aa459 --- /dev/null +++ b/inventory/hosts.ini @@ -0,0 +1,24 @@ +# Groups +[keepalived:children] +master +backup + +[keepalived:vars] +ansible_user=saansi-bot +keepalived_process_tracker=k3s-server +keepalived_nic=eth0 +keepalived_auth_pass=password +keepalived_vip=172.22.0.100/22 +keepalived_master_hostname=srvk3s01.voser.local + + +# Hosts +[master] +172.22.0.101 +[backup] +172.22.0.102 +172.22.0.103 +172.22.0.104 +172.22.0.121 +172.22.0.122 +172.22.0.123 \ No newline at end of file diff --git a/remove.yaml b/remove.yaml new file mode 100644 index 0000000..4891e6c --- /dev/null +++ b/remove.yaml @@ -0,0 +1,7 @@ +--- + +- hosts: keepalived + gather_facts: yes + become: yes + roles: + - role: remove \ No newline at end of file diff --git a/roles/download/tasks/main.yaml b/roles/download/tasks/main.yaml new file mode 100644 index 0000000..1c8b9aa --- /dev/null +++ b/roles/download/tasks/main.yaml @@ -0,0 +1,12 @@ +--- + +- name: Update Apt-Cache + apt: + update_cache: yes + cache_valid_time: 3600 + when: ansible_os_family == 'Debian' + +- name: Install keepalived + package: + name: keepalived + state: latest diff --git a/roles/keepalived/backup/tasks/main.yaml b/roles/keepalived/backup/tasks/main.yaml new file mode 100644 index 0000000..8d7c8c3 --- /dev/null +++ b/roles/keepalived/backup/tasks/main.yaml @@ -0,0 +1,19 @@ +--- + +- name: Configure keepalived + template: + src: "keepalived.conf.j2" + dest: /etc/keepalived/keepalived.conf + register: template + +- name: Restart keepalive Server + service: + name: keepalived + state: restarted + when: template.changed + +- name: Running keepalive Server + service: + name: keepalived + state: started + enabled: yes \ No newline at end of file diff --git a/roles/keepalived/backup/templates/keepalived.conf.j2 b/roles/keepalived/backup/templates/keepalived.conf.j2 new file mode 100644 index 0000000..fe5d3de --- /dev/null +++ b/roles/keepalived/backup/templates/keepalived.conf.j2 @@ -0,0 +1,24 @@ +vrrp_track_process track_process { + process "{{ keepalived_process_tracker }}" + weight 100 +} + +vrrp_instance rpis_vip { + state BACKUP + + interface {{ keepalived_nic }} + virtual_router_id 1{{ ansible_hostname[-2] }} + priority 10 + + advert_int 1 + authentication { + auth_type PASS + auth_pass {{ keepalived_auth_pass }} + } + virtual_ipaddress { + {{ keepalived_vip }} + } + track_process { + track_process + } +} \ No newline at end of file diff --git a/roles/keepalived/master/tasks/main.yaml b/roles/keepalived/master/tasks/main.yaml new file mode 100644 index 0000000..8d7c8c3 --- /dev/null +++ b/roles/keepalived/master/tasks/main.yaml @@ -0,0 +1,19 @@ +--- + +- name: Configure keepalived + template: + src: "keepalived.conf.j2" + dest: /etc/keepalived/keepalived.conf + register: template + +- name: Restart keepalive Server + service: + name: keepalived + state: restarted + when: template.changed + +- name: Running keepalive Server + service: + name: keepalived + state: started + enabled: yes \ No newline at end of file diff --git a/roles/keepalived/master/templates/keepalived.conf.j2 b/roles/keepalived/master/templates/keepalived.conf.j2 new file mode 100644 index 0000000..32299d4 --- /dev/null +++ b/roles/keepalived/master/templates/keepalived.conf.j2 @@ -0,0 +1,24 @@ +vrrp_track_process track_process { + process "{{ keepalived_process_tracker }}" + weight 100 +} + +vrrp_instance rpis_vip { + state MASTER + + interface {{ keepalived_nic }} + virtual_router_id 1{{ ansible_hostname[-2] }} + priority 11 + + advert_int 1 + authentication { + auth_type PASS + auth_pass {{ keepalived_auth_pass }} + } + virtual_ipaddress { + {{ keepalived_vip }} + } + track_process { + track_process + } +} \ No newline at end of file diff --git a/roles/prereq/tasks/main.yml b/roles/prereq/tasks/main.yml new file mode 100644 index 0000000..f66401e --- /dev/null +++ b/roles/prereq/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Set SELinux to disabled state + selinux: + state: disabled + when: ansible_distribution in ['CentOS', 'Red Hat Enterprise Linux'] \ No newline at end of file diff --git a/roles/remove/tasks/main.yaml b/roles/remove/tasks/main.yaml new file mode 100644 index 0000000..dd783cd --- /dev/null +++ b/roles/remove/tasks/main.yaml @@ -0,0 +1,26 @@ +--- +- name: Disable services + systemd: + name: "{{ item }}" + state: stopped + enable: no + failed_when: false + with_items: + - keepalived + +- name: Install keepalived + package: + name: keepalived + state: absent + +- name: Remove files + file: + name: "{{ item }}" + state: absent + with_items: + - "/etc/keepalived/keepalived.conf" + - "/etc/keepalived" + +- name: daemon_reload + systemd: + daemon_reload: yes \ No newline at end of file