Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replication role detection from runtime Redis information #128

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ suites:
- name: sentinel
- name: checksum
- name: service-name
- name: facts
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,11 @@ The following facts are accessible in your inventory or tasks outside of this ro
- `{{ ansible_local.redis.sentinel_port }}`
- `{{ ansible_local.redis.sentinel_monitors }}`

Variables provided by the 'info' Redis command are also made available for each
configured server or sentinel instance. For example:

- `{{ ansible_local.redis_6379.redis_version }}`
- `{{ ansible_local.redis_6379.role }}`
- `{{ ansible_local.redis_sentinel_26379.sentinel_masters }}`

To disable these facts, set `redis_local_facts` to a false value.
8 changes: 7 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ redis_socket_perm: 755
## Replication options
# Set slaveof just as you would in redis.conf. (e.g. "redis01 6379")
redis_slaveof: false
# Ignore manually assigned roles for instances if runtime status can be used instead
redis_runtime_replication_role: yes
# Make slaves read-only. "yes" or "no"
redis_slave_read_only: "yes"
redis_slave_priority: 100
Expand All @@ -55,8 +57,10 @@ redis_syslog_ident: "{{ redis_service_name }}"
redis_syslog_facility: USER

## General configuration
redis_daemonize: "yes"
redis_daemonize: "{{ 'no' if ansible_service_mgr|default() == 'systemd' else 'yes' }}"
redis_pidfile: /var/run/redis/{{ redis_port }}.pid
redis_dynamic_config_file: /etc/redis/{{ redis_port }}.run.conf

# Number of databases to allow
redis_databases: 16
redis_loglevel: notice
Expand Down Expand Up @@ -92,6 +96,8 @@ redis_sentinel_bind: 0.0.0.0
redis_sentinel_port: 26379
redis_sentinel_pidfile: /var/run/redis/sentinel_{{ redis_sentinel_port }}.pid
redis_sentinel_logfile: '""'
redis_sentinel_dynamic_config_file: /etc/redis/sentinel_{{ redis_sentinel_port }}.run.conf

redis_sentinel_syslog_ident: sentinel_{{ redis_sentinel_port }}
redis_sentinel_monitors:
- name: master01
Expand Down
17 changes: 12 additions & 5 deletions tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@
path: "{{ redis_install_dir }}"
state: directory

- name: create /etc/redis
file:
path: /etc/redis
state: directory

- name: add redis user
user:
name: "{{ redis_user }}"
Expand All @@ -104,6 +99,12 @@
shell: /bin/false
system: yes

- name: create /etc/redis
file:
path: /etc/redis
state: directory
owner: "{{ redis_user }}"

- name: create /var/run/redis
file:
path: /var/run/redis
Expand All @@ -115,3 +116,9 @@
args:
chdir: /usr/local/src/redis-{{ redis_version }}
creates: "{{ redis_install_dir }}/bin/redis-server"

- name: add config preparation script
template:
dest: "{{ redis_install_dir }}/bin/prepare-config.sh"
src: prepare-config.sh.j2
mode: 0755
21 changes: 21 additions & 0 deletions tasks/local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@
file:
path: /etc/ansible/facts.d
state: directory
mode: 0755

- name: create redis facts
template:
src: etc/ansible/facts.d/redis.fact.j2
dest: /etc/ansible/facts.d/redis.fact

- name: create redis instance facts
template:
src: etc/ansible/facts.d/redis_instance.fact.j2
dest: /etc/ansible/facts.d/redis_{{ redis_port }}.fact
mode: 0755
when: not redis_sentinel
register: redis_facts_install_result

- name: create redis sentinel facts
template:
src: etc/ansible/facts.d/redis_instance.fact.j2
dest: /etc/ansible/facts.d/redis_sentinel_{{ redis_sentinel_port }}.fact
mode: 0755
when: redis_sentinel
register: redis_sentinel_facts_install_result

- name: refresh facts due to new fact scripts
setup:
when: redis_facts_install_result.changed or redis_sentinel_facts_install_result.changed
8 changes: 6 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
---
- include: check_vars.yml

- include: local_facts.yml
when: redis_local_facts|bool

- include: runtime_roles.yml
when: not redis_sentinel and redis_runtime_replication_role

- include: install.yml

- include: server.yml
Expand All @@ -13,5 +19,3 @@
tags:
- config

- include: local_facts.yml
when: redis_local_facts|bool
16 changes: 16 additions & 0 deletions tasks/runtime_roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: set redis to slave automatically
set_fact:
redis_slaveof: >-
{{ ansible_local[redis_service_name].master_host }}
{{ ansible_local[redis_service_name].master_port }}
when: >-
redis_service_name in ansible_local and
ansible_local[redis_service_name].role|default() == 'slave'

- name: set redis to master automatically
set_fact:
redis_slaveof: ~
when: >-
redis_service_name in ansible_local and
ansible_local[redis_service_name].role|default() == 'master' and
ansible_local[redis_service_name].connected_slaves|default(0)|int > 0
11 changes: 10 additions & 1 deletion tasks/sentinel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
paths:
- ../templates
when: redis_as_service and ansible_service_mgr|default() != 'systemd'
notify: restart sentinel

- name: create sentinel systemd service
template:
Expand All @@ -33,12 +34,17 @@
paths:
- ../templates
when: redis_as_service and ansible_service_mgr|default() == 'systemd'
register: redis_sentinel_systemd_service_install
notify: restart sentinel

- name: systemd daemon reload
command: systemctl daemon-reload
when: redis_sentinel_systemd_service_install.changed

- name: set sentinel to start at boot
service:
name: sentinel_{{ redis_sentinel_port }}
enabled: yes
daemon_reload: "{{ True if ansible_service_mgr|default() == 'systemd' else omit }}"
when: redis_as_service

# Check then create log dir to prevent aggressively overwriting permissions
Expand Down Expand Up @@ -89,20 +95,23 @@
src: redis_sentinel.conf.j2
dest: /etc/redis/sentinel_{{ redis_sentinel_port }}.conf
owner: "{{ redis_user }}"
group: "{{ redis_group }}"
mode: 0640
notify: restart sentinel

- name: add sentinel init config file
template:
dest: /etc/sysconfig/sentinel_{{ redis_sentinel_port }}
src: redis.init.conf.j2
mode: 0600
when: ansible_os_family == "RedHat"
notify: restart sentinel

- name: add sentinel init config file
template:
dest: /etc/default/sentinel_{{ redis_sentinel_port }}
src: redis.init.conf.j2
mode: 0600
when: ansible_os_family == "Debian"
notify: restart sentinel

Expand Down
13 changes: 10 additions & 3 deletions tasks/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
- default/redis.init.j2
paths:
- ../templates
when: redis_as_service and ansible_service_mgr | default != 'systemd'
when: redis_as_service and ansible_service_mgr|default != 'systemd'
notify: restart redis

- name: create redis systemd service
template:
Expand All @@ -32,13 +33,18 @@
- default/redis.service.j2
paths:
- ../templates
when: redis_as_service and ansible_service_mgr | default == 'systemd'
when: redis_as_service and ansible_service_mgr|default == 'systemd'
register: redis_systemd_service_install
notify: restart redis

- name: systemd daemon reload
command: systemctl daemon-reload
when: redis_systemd_service_install.changed

- name: set redis to start at boot
service:
name: "{{ redis_service_name }}"
enabled: yes
daemon_reload: "{{ True if ansible_service_mgr | default == 'systemd' else omit }}"
when: redis_as_service

# Check then create log dir to prevent aggressively overwriting permissions
Expand Down Expand Up @@ -89,6 +95,7 @@
src: redis.conf.j2
dest: /etc/redis/{{ redis_port }}.conf
owner: "{{ redis_user }}"
group: "{{ redis_group }}"
mode: 0640
notify: restart redis

Expand Down
7 changes: 6 additions & 1 deletion templates/Debian/redis.init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ PIDFILE_DIR=$(dirname "${PIDFILE}")

REDIS_USER={{ redis_user }}
CONF="/etc/redis/${REDIS_PORT}.conf"
DYN_CONF="{{ redis_dynamic_config_file }}"

CLIEXEC="{{ redis_install_dir }}/bin/redis-cli -p ${REDIS_PORT}"
PREPARE_CONFIG_SCRIPT={{ redis_install_dir }}/bin/prepare-config.sh

if [ -r /etc/default/redis_${REDIS_PORT} ]; then
. /etc/default/redis_${REDIS_PORT}
Expand All @@ -50,14 +53,16 @@ case "$1" in
ulimit -n $NOFILE_LIMIT
fi

$PREPARE_CONFIG_SCRIPT "$CONF" "$DYN_CONF"

if [ ! -d "$PIDFILE_DIR" ]; then
mkdir "$PIDFILE_DIR"
chown ${REDIS_USER}:${REDIS_USER} "$PIDFILE_DIR"
chmod 0755 "$PIDFILE_DIR"
fi

log_daemon_msg "Starting $NAME..."
if start-stop-daemon --start -q --oknodo -p "$PIDFILE" -c $REDIS_USER --exec $DAEMON -- $CONF; then
if start-stop-daemon --start -q --oknodo -p "$PIDFILE" -c $REDIS_USER --exec $DAEMON -- $DYN_CONF; then
log_end_msg 0
else
log_end_msg 1
Expand Down
5 changes: 5 additions & 0 deletions templates/Debian/redis_sentinel.init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ PIDFILE_DIR=$(dirname "${PIDFILE}")

REDIS_USER={{ redis_user }}
CONF="/etc/redis/sentinel_${SENTINEL_PORT}.conf"
DYN_CONF="{{ redis_sentinel_dynamic_config_file }}"

CLIEXEC="{{ redis_install_dir }}/bin/redis-cli -p ${SENTINEL_PORT}"
PREPARE_CONFIG_SCRIPT={{ redis_install_dir }}/bin/prepare-config.sh

if [ -r /etc/default/sentinel_${SENTINEL_PORT} ]; then
. /etc/default/sentinel_${SENTINEL_PORT}
Expand All @@ -50,6 +53,8 @@ case "$1" in
ulimit -n $NOFILE_LIMIT
fi

$PREPARE_CONFIG_SCRIPT "$CONF" "$DYN_CONF"

if [ ! -d "$PIDFILE_DIR" ]; then
mkdir "$PIDFILE_DIR"
chown ${REDIS_USER}:${REDIS_USER} "$PIDFILE_DIR"
Expand Down
7 changes: 6 additions & 1 deletion templates/RedHat/redis.init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ fi
REDIS_USER={{ redis_user }}
PIDFILE={{ redis_pidfile }}
CONF="/etc/redis/${REDIS_PORT}.conf"
DYN_CONF="{{ redis_dynamic_config_file }}"
EXEC={{ redis_install_dir }}/bin/redis-server
CLIEXEC="{{ redis_install_dir }}/bin/redis-cli -p ${REDIS_PORT}"
PREPARE_CONFIG_SCRIPT={{ redis_install_dir }}/bin/prepare-config.sh

if [ -n "$REDIS_PASSWORD" ]; then
CLIEXEC="${CLIEXEC} -a ${REDIS_PASSWORD}"
Expand All @@ -38,8 +40,11 @@ case "$1" in
if [ -n "$NOFILE_LIMIT" ]; then
ulimit -n $NOFILE_LIMIT
fi

$PREPARE_CONFIG_SCRIPT "$CONF" "$DYN_CONF"

echo "Starting Redis server..."
daemon --user $REDIS_USER $EXEC $CONF
daemon --user $REDIS_USER $EXEC $DYN_CONF
fi
;;
stop)
Expand Down
7 changes: 6 additions & 1 deletion templates/RedHat/redis_sentinel.init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ REDIS_USER={{ redis_user }}
BIND_ADDRESS={{ redis_sentinel_bind }}
PIDFILE={{ redis_sentinel_pidfile }}
CONF="/etc/redis/sentinel_${SENTINEL_PORT}.conf"
DYN_CONF="{{ redis_sentinel_dynamic_config_file }}"
EXEC={{ redis_install_dir }}/bin/redis-server
CLIEXEC="{{ redis_install_dir }}/bin/redis-cli -p ${SENTINEL_PORT}"
PREPARE_CONFIG_SCRIPT={{ redis_install_dir }}/bin/prepare-config.sh

if [ -n "$REDIS_PASSWORD" ]; then
CLIEXEC="${CLIEXEC} -a ${REDIS_PASSWORD}"
Expand All @@ -39,8 +41,11 @@ case "$1" in
if [ -n "$NOFILE_LIMIT" ]; then
ulimit -n $NOFILE_LIMIT
fi

$PREPARE_CONFIG_SCRIPT "$CONF" "$DYN_CONF"

echo "Starting Redis Sentinel..."
daemon --user $REDIS_USER $EXEC $CONF --sentinel
daemon --user $REDIS_USER $EXEC $DYN_CONF --sentinel
fi
;;
stop)
Expand Down
7 changes: 6 additions & 1 deletion templates/default/redis.init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
REDIS_PORT={{ redis_port }}
REDIS_USER={{ redis_user }}
EXEC={{ redis_install_dir }}/bin/redis-server
PREPARE_CONFIG_SCRIPT={{ redis_install_dir }}/bin/prepare-config.sh
{% if redis_password -%}
CLIEXEC='{{ redis_install_dir }}/bin/redis-cli -a {{ redis_password }}'
{% else -%}
Expand All @@ -14,6 +15,7 @@ CLIEXEC={{ redis_install_dir }}/bin/redis-cli

PIDFILE={{ redis_pidfile }}
CONF="/etc/redis/${REDIS_PORT}.conf"
DYN_CONF="{{ redis_dynamic_config_file }}"

case "$1" in
start)
Expand All @@ -22,8 +24,11 @@ case "$1" in
echo "$PIDFILE exists, process is already running or crashed"
else
ulimit -n {{ redis_nofile_limit }}

$PREPARE_CONFIG_SCRIPT "$CONF" "$DYN_CONF"

echo "Starting Redis server..."
su $REDIS_USER -c "$EXEC $CONF"
su $REDIS_USER -c "$EXEC $DYN_CONF"
fi
;;
stop)
Expand Down
3 changes: 2 additions & 1 deletion templates/default/redis.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Documentation=http://redis.io/documentation, man:redis-server(1)
Type={{ 'forking' if redis_daemonize == 'yes' else 'simple' }}
ExecStartPre=-/bin/mkdir {{ redis_pidfile|dirname }}
ExecStartPre=/bin/chown -R {{ redis_user }}:{{ redis_group }} {{ redis_pidfile|dirname }}
ExecStart={{ redis_install_dir }}/bin/redis-server /etc/redis/{{ redis_port }}.conf
ExecStartPre={{ redis_install_dir }}/bin/prepare-config.sh /etc/redis/{{ redis_port }}.conf {{ redis_dynamic_config_file }}
ExecStart="{{ redis_install_dir }}/bin/redis-server" "{{ redis_dynamic_config_file }}"
EnvironmentFile=-/etc/default/redis_{{ redis_port }}
PIDFile={{ redis_pidfile }}
TimeoutStopSec=0
Expand Down
Loading