diff --git a/defaults/main.yml b/defaults/main.yml index 405ffa79..97e55727 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,7 +4,7 @@ redis_version: 2.8.24 redis_install_dir: /opt/redis redis_dir: /var/lib/redis/{{ redis_port }} redis_config_file_name: "{{ redis_port }}.conf" -redis_download_url: "http://download.redis.io/releases/redis-{{ redis_version }}.tar.gz" +redis_download_url: "https://download.redis.io/releases/redis-{{ redis_version }}.tar.gz" redis_protected_mode: "yes" # Set this to true to validate redis tarball checksum against vars/main.yml @@ -117,10 +117,17 @@ redis_hz: 10 # io-threads-do-reads yes redis_config_additional: "" +## Redis cluster configs +redis_cluster_enabled: false +redis_cluster_node_timeout: 15000 +# Prevent slave to be promoted as a master in case of auto-failover +# only works in version 4.x and above +redis_cluster_slave_no_failover: "no" + ## Redis sentinel configs # Set this to true on a host to configure it as a Sentinel redis_sentinel: false -redis_sentinel_protected_mode: "yes" +redis_sentinel_protected_mode: "no" redis_sentinel_dir: /var/lib/redis/sentinel_{{ redis_sentinel_port }} redis_sentinel_bind: 0.0.0.0 redis_sentinel_port: 26379 @@ -129,15 +136,20 @@ redis_sentinel_pidfile: /var/run/redis/sentinel_{{ redis_sentinel_port }}.pid redis_sentinel_logfile: '""' redis_sentinel_syslog_ident: sentinel_{{ redis_sentinel_port }} redis_sentinel_oom_score_adjust: 0 -redis_sentinel_monitors: - - name: master01 - host: localhost - port: 6379 - quorum: 2 - auth_pass: ant1r3z - down_after_milliseconds: 30000 - parallel_syncs: 1 - failover_timeout: 180000 - notification_script: false - client_reconfig_script: false - rename_commands: [] +redis_sentinel_monitors: [] +# Set this to true if you update the configuration via runtime +# e.g: registering master to monitor via command-line instead manually edit the config file +redis_sentinel_runtime_monitor_master: false +# Example: +# redis_sentinel_monitors: +# - name: master01 +# host: localhost +# port: 6379 +# quorum: 2 +# auth_pass: ant1r3z +# down_after_milliseconds: 30000 +# parallel_syncs: 1 +# failover_timeout: 180000 +# notification_script: false +# client_reconfig_script: false +# rename_commands: [] diff --git a/tasks/sentinel.yml b/tasks/sentinel.yml index dc389d57..907bbb37 100644 --- a/tasks/sentinel.yml +++ b/tasks/sentinel.yml @@ -102,6 +102,32 @@ - redis_sentinel_pidfile != '""' - not sentinel_piddir.stat.exists +- name: check if sentinel is running + command: + cmd: systemctl status sentinel_{{ redis_sentinel_port }} + ignore_errors: true + changed_when: false + register: sentinel_service_status + +- name: run flushconfig to rewrite config file from current configuration + command: + cmd: "redis-cli -p {{ redis_sentinel_port }} SENTINEL FLUSHCONFIG" + when: sentinel_service_status.rc == 0 + +- name: check if sentinel config file exists + stat: + path: /etc/redis/sentinel_{{ redis_sentinel_port }}.conf + register: _config_file + +- name: backup existing config file + copy: + src: /etc/redis/sentinel_{{ redis_sentinel_port }}.conf + dest: /etc/redis/sentinel_{{ redis_sentinel_port }}.conf.{{ ansible_date_time['date'] }} + owner: "{{ redis_user }}" + mode: 0640 + remote_src: true + when: _config_file.stat.exists + - name: create sentinel config file template: src: redis_sentinel.conf.j2 @@ -109,6 +135,53 @@ owner: "{{ redis_user }}" mode: 0640 notify: "restart sentinel" + when: + - not redis_sentinel_runtime_monitor_master or + not _config_file.stat.exists + +- name: update static configuration in config file + blockinfile: + path: /etc/redis/sentinel_{{ redis_sentinel_port }}.conf + insertbefore: "### STATIC CONFIG STOP HERE ###" + owner: "{{ redis_user }}" + mode: 0640 + backup: yes + block: | + # redis-sentinel {{ redis_version }} configuration file + # sentinel_{{ redis_sentinel_port }}.conf + + daemonize {{ redis_daemonize }} + protected-mode {{ redis_sentinel_protected_mode }} + dir {{ redis_sentinel_dir }} + pidfile {{ redis_sentinel_pidfile }} + port {{ redis_sentinel_port }} + bind {{ redis_sentinel_bind }} + + # Security + {% if redis_sentinel_password %} + requirepass {{ redis_sentinel_password }} + {% endif %} + + {% for master in redis_sentinel_monitors -%} + sentinel monitor {{ master.name }} {{ master.host }} {{ master.port }} {{ master.quorum|d('2') }} + {% for option in ('auth_pass', 'down_after_milliseconds', 'parallel_syncs', 'failover_timeout', 'notification_script', 'client_reconfig_script') -%} + {% if master[option] is defined and master[option] -%} + sentinel {{ option|replace('_', '-') }} {{ master.name }} {{ master[option] }} + {% endif %} + {% endfor -%} + {% if master['rename_commands'] is defined -%} + {% for command in master['rename_commands'] -%} + sentinel rename-command {{ master.name }} {{ command }} + {% endfor -%} + {% endif -%} + {% endfor -%} + + logfile {{ redis_sentinel_logfile }} + syslog-enabled {{ redis_syslog_enabled }} + syslog-ident {{ redis_sentinel_syslog_ident }} + syslog-facility {{ redis_syslog_facility }} + notify: "restart sentinel" + when: redis_sentinel_runtime_monitor_master == true - name: add sentinel init config file template: diff --git a/templates/redis.conf.j2 b/templates/redis.conf.j2 index 04e24a4f..c709e30c 100644 --- a/templates/redis.conf.j2 +++ b/templates/redis.conf.j2 @@ -53,6 +53,16 @@ min-slaves-max-lag {{ redis_min_slaves_max_lag }} masterauth {{ redis_password }} {% endif -%} +{% if redis_cluster_enabled %} +# Cluster +cluster-enabled yes +cluster-config-file nodes-{{ redis_port }}.conf +cluster-node-timeout {{ redis_cluster_node_timeout }} +{% if redis_version.split('.') | first | int >= 4 -%} +cluster-slave-no-failover {{ redis_cluster_slave_no_failover }} +{% endif -%} +{% endif -%} + # Security {% if redis_password -%} requirepass {{ redis_password }} @@ -104,4 +114,4 @@ aof-rewrite-incremental-fsync yes {% if redis_config_additional|length -%} # Additional {{ redis_config_additional -}} -{% endif -%} \ No newline at end of file +{% endif -%} diff --git a/templates/redis_sentinel.conf.j2 b/templates/redis_sentinel.conf.j2 index 2fc59390..919ddb34 100644 --- a/templates/redis_sentinel.conf.j2 +++ b/templates/redis_sentinel.conf.j2 @@ -1,3 +1,4 @@ +### STATIC CONFIG START HERE ### # redis-sentinel {{ redis_version }} configuration file # sentinel_{{ redis_sentinel_port }}.conf @@ -31,3 +32,5 @@ logfile {{ redis_sentinel_logfile }} syslog-enabled {{ redis_syslog_enabled }} syslog-ident {{ redis_sentinel_syslog_ident }} syslog-facility {{ redis_syslog_facility }} + +### STATIC CONFIG STOP HERE ###