Import the old keepalived role.

This commit is contained in:
Andrea Dell'Amico 2020-06-17 18:47:15 +02:00
parent 1211de805c
commit fe375e7562
11 changed files with 428 additions and 68 deletions

View File

@ -1,31 +1,79 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
A role that installs and configures keepalived.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
The most important variables are listed below:
``` yaml
keepalived_install: True
keepalived_enabled: '{{ keepalived_install }}'
keepalived_version: 1.1.20
keepalived_repo: 'ppa:keepalived/stable'
keepalived_pkg_state: latest
# Use 'keepalived={{ keepalived_version }}' if you want a specific version
keepalived_pkgs:
- keepalived
keepalived_use_unicast: False
keepalived_unicast_src_ip: '{{ ansible_default_ipv4.address }}'
keepalived_unicast_peer_ip:
- 127.0.0.1
- 127.0.0.2
keepalived_mcast_addr: 224.0.0.0/8
keepalived_floating_ip1: 127.0.0.1
keepalived_script1_name: keepalived_check_service_status
# We set haproxy as the default because most of our keepalived use is haproxy related
keepalived_service_to_check: 'haproxy'
keepalived_script1_check: '/bin/pidof {{ keepalived_service_to_check }}'
keepalived_instance1_name: 'VRRP_1'
keepalived_interface1_name: '{{ ansible_default_ipv4.interface }}'
keepalived_initial_state: 'MASTER'
keepalived_inst_priority: 100
keepalived_vrouter_id: 51
keepalived_non_local_bind: True
keepalived_enable_script_security: True
keepalived_script_username: keepalived_script
keepalived_script_user_home: '/var/lib/keepalived'
keepalived_runtime_dir: /run/keepalived
keepalived_nagios_check: False
keepalived_notify_script: /usr/local/bin/keepalived_notify
keepalived_check_script: '/usr/local/bin/{{ keepalived_script1_name }}'
# The service must install its own script if it is too complex to be managed by the provided one
keepalived_scripts:
- name: '{{ keepalived_script1_name }}'
script: '{{ keepalived_check_script }}'
# In seconds
interval: 2
# Priority increment
weight: 2
keepalived_instances:
- name: '{{ keepalived_instance1_name }}'
interface: '{{ keepalived_interface1_name }}'
state: '{{ keepalived_initial_state }}'
vrouter_id: '{{ keepalived_vrouter_id }}'
notify: '{{ keepalived_notify_script }}'
priority: '{{ keepalived_inst_priority }}'
nopreempt: True
v_addr:
- '{{ keepalived_floating_ip1 }}'
track_script:
- '{{ keepalived_script1_name }}'
track_interface:
- '{{ keepalived_interface1_name }}'
```
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
None
License
-------
@ -35,4 +83,4 @@ EUPL-1.2
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).
Andrea Dell'Amico, <andrea.dellamico@isti.cnr.it>

View File

@ -1,2 +1,62 @@
---
# defaults file for ansible-role-template
# http://www.keepalived.org/
#
keepalived_install: True
keepalived_enabled: '{{ keepalived_install }}'
keepalived_version: 1.1.20
keepalived_repo: 'ppa:keepalived/stable'
keepalived_pkg_state: latest
# Use 'keepalived={{ keepalived_version }}' if you want a specific version
keepalived_pkgs:
- keepalived
keepalived_use_unicast: False
keepalived_unicast_src_ip: '{{ ansible_default_ipv4.address }}'
keepalived_unicast_peer_ip:
- 127.0.0.1
- 127.0.0.2
keepalived_mcast_addr: 224.0.0.0/8
keepalived_floating_ip1: 127.0.0.1
keepalived_script1_name: keepalived_check_service_status
# We set haproxy as the default because most of our keepalived use is haproxy related
keepalived_service_to_check: 'haproxy'
keepalived_script1_check: '/bin/pidof {{ keepalived_service_to_check }}'
keepalived_instance1_name: 'VRRP_1'
keepalived_interface1_name: '{{ ansible_default_ipv4.interface }}'
keepalived_initial_state: 'MASTER'
keepalived_inst_priority: 100
keepalived_vrouter_id: 51
keepalived_non_local_bind: True
keepalived_enable_script_security: True
keepalived_script_username: keepalived_script
keepalived_script_user_home: '/var/lib/keepalived'
keepalived_runtime_dir: /run/keepalived
keepalived_nagios_check: False
keepalived_notify_script: /usr/local/bin/keepalived_notify
keepalived_check_script: '/usr/local/bin/{{ keepalived_script1_name }}'
# The service must install its own script if it is too complex to be managed by the provided one
keepalived_scripts:
- name: '{{ keepalived_script1_name }}'
script: '{{ keepalived_check_script }}'
# In seconds
interval: 2
# Priority increment
weight: 2
keepalived_instances:
- name: '{{ keepalived_instance1_name }}'
interface: '{{ keepalived_interface1_name }}'
state: '{{ keepalived_initial_state }}'
vrouter_id: '{{ keepalived_vrouter_id }}'
notify: '{{ keepalived_notify_script }}'
priority: '{{ keepalived_inst_priority }}'
nopreempt: True
v_addr:
- '{{ keepalived_floating_ip1 }}'
track_script:
- '{{ keepalived_script1_name }}'
track_interface:
- '{{ keepalived_interface1_name }}'

View File

@ -0,0 +1,57 @@
#!/bin/bash
#
# echo $1 $2 is in $3 state > /var/run/keepalive.state
#
###############################################################
# Check Keepalived State #
# #
# Author: Zhivko Todorov <ztodorov@neterra.net> #
# Date: 01-Dec-2015 #
# Version: 0.0.1 #
# License: GPL #
###############################################################
# set to 'true' if the host is supposed to be in MASTER state
# or set to 'false' if the host is supposed to be in BACKUP state
# nrpe cannot receive external variables UNLESS is forced in config
MASTER='true'
# checking if there are alive keepalived processes so we can trust the content of the notify 'state' file
KEEPALIVENUM=`ps uax|grep '/usr/sbin/keepalived'|grep -v grep|wc -l|tr -d "\n"`
if [ $KEEPALIVENUM -gt 0 ]; then
KEEPALIVESTATE=`cat /var/run/keepalive.state`
if [ "$MASTER" == "true" ]; then
if [[ $KEEPALIVESTATE == *"MASTER"* ]];then
echo $KEEPALIVESTATE
exit 0
fi
if [[ $KEEPALIVESTATE == *"BACKUP"* ]];then
echo $KEEPALIVESTATE
exit 0
fi
else
if [[ $KEEPALIVESTATE == *"BACKUP"* ]];then
echo $KEEPALIVESTATE
exit 0
fi
if [[ $KEEPALIVESTATE == *"MASTER"* ]];then
echo $KEEPALIVESTATE
exit 2
fi
fi
fi
echo "Keepalived is in UNKNOWN state"
exit 3

View File

@ -1,2 +1,4 @@
---
# handlers file for ansible-role-template
- name: restart keepalived
service: name=keepalived state=restarted
when: keepalived_enabled

View File

@ -1,61 +1,23 @@
galaxy_info:
author: your name
description: your description
author: Andrea Dell'Amico
description: Systems Architect
company: ISTI-CNR
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
issue_tracker_url: https://redmine-s2i2s.isti.cnr.it/projects/provisioning
# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2
# - GPLv3
# - Apache
# - CC-BY
license: EUPL-1.2
license: EUPL 1.2+
min_ansible_version: 2.8
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
# Optionally specify the branch Galaxy will use when accessing the GitHub
# repo for this role. During role install, if no tags are available,
# Galaxy will use this branch. During import Galaxy will access files on
# this branch. If Travis integration is configured, only notifications for this
# branch will be accepted. Otherwise, in all cases, the repo's default branch
# (usually master) will be used.
#github_branch:
#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99
platforms:
- name: Ubuntu
versions:
- bionic
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
galaxy_tags:
- keepalived
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -1,2 +1,100 @@
---
# tasks file for ansible-role-template
- block:
- name: Install the keepalived repository
apt_repository: repo={{ keepalived_repo }} update_cache=yes
when: ansible_distribution_major_version <= '16'
- name: Install the keepalived package
apt: name={{ keepalived_pkgs }} state={{ keepalived_pkg_state }} cache_valid_time=1800
tags: keepalived
when:
- keepalived_install
- ansible_distribution_file_variety == "Debian"
- block:
- name: Install the user that the keepalived scripts will run under
user: name={{ keepalived_script_username }} home={{ keepalived_script_user_home }} createhome=no shell=/usr/sbin/nologin system=yes
- name: Create the keepalive script user directory
file: dest={{ keepalived_script_user_home }} state=directory owner={{ keepalived_script_username }} group={{ keepalived_script_username }} mode=0750
- name: Create the tmpfile entry for the keepalived runtime directory
template: src=tmpfile_keepalived.conf.j2 dest=/usr/lib/tmpfiles.d/keepalived.conf owner=root group=root mode=0644
when: ansible_service_mgr == 'systemd'
- name: Install the keepalived configuration
template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf owner=root group=root mode=0600
notify: restart keepalived
- name: Install the keepalived default check script
template: src=keepalived_check_service_status.sh.j2 dest=/usr/local/bin/keepalived_check_service_status owner=root group=root mode=0755
notify: restart keepalived
- name: Install the keepalived notify scripts
template: src=keepalived_notify.sh.j2 dest={{ item.notify }} owner=root group={{ keepalived_script_username }} mode=0754
with_items: '{{ keepalived_instances }}'
- name: Set the kernel net.ipv4.ip_nonlocal_bind sysctl
sysctl:
reload: yes
state: present
name: net.ipv4.ip_nonlocal_bind
sysctl_set: yes
sysctl_file: /etc/sysctl.d/90-keepalived
value: 1
when: keepalived_non_local_bind | bool
- name: Disable the kernel net.ipv4.ip_nonlocal_bind sysctl if not needed
sysctl:
reload: yes
state: absent
name: net.ipv4.ip_nonlocal_bind
sysctl_set: yes
sysctl_file: /etc/sysctl.d/90-keepalived
value: 1
when: not keepalived_non_local_bind
- name: Ensure that keepalived is started and enabled
service: name=keepalived state=started enabled=yes
when: keepalived_enabled
- name: Ensure that keepalived is stopped and disabled
service: name=keepalived state=stopped enabled=no
when: not keepalived_enabled
tags: [ 'keepalived', 'keepalived_conf' ]
when: keepalived_install
- block:
- name: Install the keepalived NRPE nagios check
copy: src=check_keepalived_state dest={{ nagios_plugins_dir }}/check_keepalived_state owner=root group=root mode=0555
with_items: '{{ keepalived_instances }}'
- name: Install the keepalived NRPE command configuration
template: src=keepalived-nrpe.cfg.j2 dest={{ nrpe_include_dir }}/keepalived-nrpe.cfg owner=root group=root mode=0444
notify: Reload NRPE server
tags: keepalived
when:
- keepalived_install
- keepalived_nagios_check
- block:
- name: Remove the keepalived package if we do not want it
apt: name={{ keepalived_pkgs }} state=absent
- name: Remove the keepalived notify scripts
file: dest={{ item.notify }} state=absent
with_items: '{{ keepalived_instances }}'
- name: Remove the keepalived NRPE check
file: dest={{ nagios_plugins_dir }}/check_keepalived_state state=absent
with_items: '{{ keepalived_instances }}'
- name: Remove the keepalived NRPE command configuration
file: dest={{ nrpe_include_dir }}/keepalived-nrpe.cfg state=absent
tags: keepalived
when: not keepalived_install

View File

@ -0,0 +1,6 @@
#
command[check_keepalived]={{ nagios_plugins_dir }}/check_keepalived_state
# Restart keepalived (via handler)
command[global_restart_keepalived]=/usr/bin/sudo /etc/init.d/keepalived restart

View File

@ -0,0 +1,88 @@
global_defs {
{% if keepalived_enable_script_security %}
enable_script_security
{% if ansible_distribution_file_variety == "Debian" and ansible_distribution_major_version == '18' %}
script_user root
{% else %}
script_user {{ keepalived_script_username }}
{% endif %}
{% endif %}
}
{% for script in keepalived_scripts %}
vrrp_script {{ script.name }} {
script "{{ script.script }}"
interval {{ script.interval | default(1) }}
weight {{ script.weight | default(2) }}
fall {{ script.fall | default(3) }}
rise {{ script.raise | default(2) }}
timeout {{ script.timeout | default(5) }}
}
{% endfor %}
{% for instance in keepalived_instances %}
vrrp_instance {{ instance.name }} {
interface {{ instance.interface }}
{% if instance.state is defined %}
{% if instance.nopreempt is defined and instance.nopreempt %}
state BACKUP
{% else %}
state {{ instance.state }}
{% endif %}
{% endif %}
virtual_router_id {{ instance.vrouter_id }}
priority {{ instance.priority }}
{% if instance.notify is defined %}
notify {{ instance.notify }}
{% endif %}
{% if instance.nopreempt is defined and instance.nopreempt %}
nopreempt
{% endif %}
{% if instance.authentication is defined %}
authentication {
auth_type {{ instance.authentication }}
auth_pass {{ instance.authpass }}
}
{% endif %}
virtual_ipaddress {
{% for addr in instance.v_addr %}
{{ addr }}
{% endfor %}
}
track_script {
{% for trk in instance.track_script %}
{{ trk }}
{% endfor %}
}
{% if instance.track_interface is defined %}
track_interface {
{% for if in instance.track_interface %}
{{ if }}
{% endfor %}
}
{% endif %}
{% if keepalived_use_unicast %}
unicast_src_ip {{ instance.keepalived_unicast_src_ip }}
unicast_peer {
{% for ipaddr in instance.keepalived_unicast_peer_ip %}
{{ ipaddr }}
{% endfor %}
}
{% endif %}
}
{% endfor %}

View File

@ -0,0 +1,19 @@
#!/bin/bash
{% if ansible_distribution_file_variety == "Debian" and ansible_distribution_major_version == '18' %}
am_i_master=$( grep MASTER {{ keepalived_runtime_dir }}/keepalive.state )
keepalive_master=$?
if [ $keepalive_master -eq 0 ] ; then
{% for instance in keepalived_instances %}
{% for addr in instance.v_addr %}
float_ip=$( ip addr show | grep {{ addr }} )
retval=$?
[ $retval -eq 1 ] && /bin/systemctl restart keepalived
{% endfor %}
{% endfor %}
fi
{% endif %}
{{ keepalived_script1_check }}
exit $?

View File

@ -0,0 +1,19 @@
#!/bin/bash
#
# echo $1 $2 is in $3 state > /var/lib/keepalived/keepalive.state
#
umask 022
export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:$PATH"
TYPE=$1
NAME=$2
STATE=$3
RETVAL=0
{% if keepalived_notify_extra_params is defined %}
{{ keepalived_notify_extra_params }}
{% endif %}
echo "$TYPE $NAME is in $STATE state" > {{ keepalived_runtime_dir }}/keepalive.state
exit $RETVAL

View File

@ -0,0 +1 @@
d {{ keepalived_runtime_dir }} 0775 {{ keepalived_script_username }} {{ keepalived_script_username }}