mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
* Redis: slave -> replica
* Fallback for old Redis versions in CI.
(cherry picked from commit 2d1f5408d3
)
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
7361ca5430
commit
769233808d
7 changed files with 70 additions and 55 deletions
2
changelogs/fragments/2867-redis-terminology.yml
Normal file
2
changelogs/fragments/2867-redis-terminology.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- "redis - allow to use the term ``replica`` instead of ``slave``, which has been the official Redis terminology since 2018 (https://github.com/ansible-collections/community.general/pull/2867)."
|
|
@ -10,17 +10,17 @@ __metaclass__ = type
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: redis
|
module: redis
|
||||||
short_description: Various redis commands, slave and flush
|
short_description: Various redis commands, replica and flush
|
||||||
description:
|
description:
|
||||||
- Unified utility to interact with redis instances.
|
- Unified utility to interact with redis instances.
|
||||||
options:
|
options:
|
||||||
command:
|
command:
|
||||||
description:
|
description:
|
||||||
- The selected redis command
|
- The selected redis command
|
||||||
- C(config) (new in 1.6), ensures a configuration setting on an instance.
|
- C(config) ensures a configuration setting on an instance.
|
||||||
- C(flush) flushes all the instance or a specified db.
|
- C(flush) flushes all the instance or a specified db.
|
||||||
- C(slave) sets a redis instance in slave or master mode.
|
- C(replica) sets a redis instance in replica or master mode. (C(slave) is an alias for C(replica).)
|
||||||
choices: [ config, flush, slave ]
|
choices: [ config, flush, replica, slave ]
|
||||||
type: str
|
type: str
|
||||||
login_password:
|
login_password:
|
||||||
description:
|
description:
|
||||||
|
@ -38,18 +38,21 @@ options:
|
||||||
type: int
|
type: int
|
||||||
master_host:
|
master_host:
|
||||||
description:
|
description:
|
||||||
- The host of the master instance [slave command]
|
- The host of the master instance [replica command]
|
||||||
type: str
|
type: str
|
||||||
master_port:
|
master_port:
|
||||||
description:
|
description:
|
||||||
- The port of the master instance [slave command]
|
- The port of the master instance [replica command]
|
||||||
type: int
|
type: int
|
||||||
slave_mode:
|
replica_mode:
|
||||||
description:
|
description:
|
||||||
- the mode of the redis instance [slave command]
|
- The mode of the redis instance [replica command]
|
||||||
default: slave
|
- C(slave) is an alias for C(replica).
|
||||||
choices: [ master, slave ]
|
default: replica
|
||||||
|
choices: [ master, replica, slave ]
|
||||||
type: str
|
type: str
|
||||||
|
aliases:
|
||||||
|
- slave_mode
|
||||||
db:
|
db:
|
||||||
description:
|
description:
|
||||||
- The database to flush (used in db mode) [flush command]
|
- The database to flush (used in db mode) [flush command]
|
||||||
|
@ -76,7 +79,7 @@ notes:
|
||||||
- Requires the redis-py Python package on the remote host. You can
|
- Requires the redis-py Python package on the remote host. You can
|
||||||
install it with pip (pip install redis) or with a package manager.
|
install it with pip (pip install redis) or with a package manager.
|
||||||
https://github.com/andymccurdy/redis-py
|
https://github.com/andymccurdy/redis-py
|
||||||
- If the redis master instance we are making slave of is password protected
|
- If the redis master instance we are making replica of is password protected
|
||||||
this needs to be in the redis.conf in the masterauth variable
|
this needs to be in the redis.conf in the masterauth variable
|
||||||
|
|
||||||
seealso:
|
seealso:
|
||||||
|
@ -86,16 +89,16 @@ author: "Xabier Larrakoetxea (@slok)"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
- name: Set local redis instance to be slave of melee.island on port 6377
|
- name: Set local redis instance to be a replica of melee.island on port 6377
|
||||||
community.general.redis:
|
community.general.redis:
|
||||||
command: slave
|
command: replica
|
||||||
master_host: melee.island
|
master_host: melee.island
|
||||||
master_port: 6377
|
master_port: 6377
|
||||||
|
|
||||||
- name: Deactivate slave mode
|
- name: Deactivate replica mode
|
||||||
community.general.redis:
|
community.general.redis:
|
||||||
command: slave
|
command: replica
|
||||||
slave_mode: master
|
replica_mode: master
|
||||||
|
|
||||||
- name: Flush all the redis db
|
- name: Flush all the redis db
|
||||||
community.general.redis:
|
community.general.redis:
|
||||||
|
@ -145,7 +148,7 @@ import re
|
||||||
|
|
||||||
|
|
||||||
# Redis module specific support methods.
|
# Redis module specific support methods.
|
||||||
def set_slave_mode(client, master_host, master_port):
|
def set_replica_mode(client, master_host, master_port):
|
||||||
try:
|
try:
|
||||||
return client.slaveof(master_host, master_port)
|
return client.slaveof(master_host, master_port)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -174,13 +177,13 @@ def flush(client, db=None):
|
||||||
def main():
|
def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
command=dict(type='str', choices=['config', 'flush', 'slave']),
|
command=dict(type='str', choices=['config', 'flush', 'replica', 'slave']),
|
||||||
login_password=dict(type='str', no_log=True),
|
login_password=dict(type='str', no_log=True),
|
||||||
login_host=dict(type='str', default='localhost'),
|
login_host=dict(type='str', default='localhost'),
|
||||||
login_port=dict(type='int', default=6379),
|
login_port=dict(type='int', default=6379),
|
||||||
master_host=dict(type='str'),
|
master_host=dict(type='str'),
|
||||||
master_port=dict(type='int'),
|
master_port=dict(type='int'),
|
||||||
slave_mode=dict(type='str', default='slave', choices=['master', 'slave']),
|
replica_mode=dict(type='str', default='replica', choices=['master', 'replica', 'slave'], aliases=["slave_mode"]),
|
||||||
db=dict(type='int'),
|
db=dict(type='int'),
|
||||||
flush_mode=dict(type='str', default='all', choices=['all', 'db']),
|
flush_mode=dict(type='str', default='all', choices=['all', 'db']),
|
||||||
name=dict(type='str'),
|
name=dict(type='str'),
|
||||||
|
@ -196,20 +199,24 @@ def main():
|
||||||
login_host = module.params['login_host']
|
login_host = module.params['login_host']
|
||||||
login_port = module.params['login_port']
|
login_port = module.params['login_port']
|
||||||
command = module.params['command']
|
command = module.params['command']
|
||||||
|
|
||||||
# Slave Command section -----------
|
|
||||||
if command == "slave":
|
if command == "slave":
|
||||||
|
command = "replica"
|
||||||
|
|
||||||
|
# Replica Command section -----------
|
||||||
|
if command == "replica":
|
||||||
master_host = module.params['master_host']
|
master_host = module.params['master_host']
|
||||||
master_port = module.params['master_port']
|
master_port = module.params['master_port']
|
||||||
mode = module.params['slave_mode']
|
mode = module.params['replica_mode']
|
||||||
|
if mode == "slave":
|
||||||
|
mode = "replica"
|
||||||
|
|
||||||
# Check if we have all the data
|
# Check if we have all the data
|
||||||
if mode == "slave": # Only need data if we want to be slave
|
if mode == "replica": # Only need data if we want to be replica
|
||||||
if not master_host:
|
if not master_host:
|
||||||
module.fail_json(msg='In slave mode master host must be provided')
|
module.fail_json(msg='In replica mode master host must be provided')
|
||||||
|
|
||||||
if not master_port:
|
if not master_port:
|
||||||
module.fail_json(msg='In slave mode master port must be provided')
|
module.fail_json(msg='In replica mode master port must be provided')
|
||||||
|
|
||||||
# Connect and check
|
# Connect and check
|
||||||
r = redis.StrictRedis(host=login_host, port=login_port, password=login_password)
|
r = redis.StrictRedis(host=login_host, port=login_port, password=login_password)
|
||||||
|
@ -223,7 +230,7 @@ def main():
|
||||||
if mode == "master" and info["role"] == "master":
|
if mode == "master" and info["role"] == "master":
|
||||||
module.exit_json(changed=False, mode=mode)
|
module.exit_json(changed=False, mode=mode)
|
||||||
|
|
||||||
elif mode == "slave" and info["role"] == "slave" and info["master_host"] == master_host and info["master_port"] == master_port:
|
elif mode == "replica" and info["role"] == "slave" and info["master_host"] == master_host and info["master_port"] == master_port:
|
||||||
status = dict(
|
status = dict(
|
||||||
status=mode,
|
status=mode,
|
||||||
master_host=master_host,
|
master_host=master_host,
|
||||||
|
@ -234,9 +241,8 @@ def main():
|
||||||
# Do the stuff
|
# Do the stuff
|
||||||
# (Check Check_mode before commands so the commands aren't evaluated
|
# (Check Check_mode before commands so the commands aren't evaluated
|
||||||
# if not necessary)
|
# if not necessary)
|
||||||
if mode == "slave":
|
if mode == "replica":
|
||||||
if module.check_mode or\
|
if module.check_mode or set_replica_mode(r, master_host, master_port):
|
||||||
set_slave_mode(r, master_host, master_port):
|
|
||||||
info = r.info()
|
info = r.info()
|
||||||
status = {
|
status = {
|
||||||
'status': mode,
|
'status': mode,
|
||||||
|
@ -245,7 +251,7 @@ def main():
|
||||||
}
|
}
|
||||||
module.exit_json(changed=True, mode=status)
|
module.exit_json(changed=True, mode=status)
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg='Unable to set slave mode')
|
module.fail_json(msg='Unable to set replica mode')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if module.check_mode or set_master_mode(r):
|
if module.check_mode or set_master_mode(r):
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
---
|
---
|
||||||
redis_password: PASS
|
redis_password: PASS
|
||||||
master_port: 6379
|
master_port: 6379
|
||||||
slave_port: 6380
|
replica_port: 6380
|
||||||
|
|
|
@ -33,9 +33,9 @@
|
||||||
- result.info.tcp_port == master_port
|
- result.info.tcp_port == master_port
|
||||||
- result.info.role == 'master'
|
- result.info.role == 'master'
|
||||||
|
|
||||||
- name: redis_info - connect to slave
|
- name: redis_info - connect to replica
|
||||||
community.general.redis_info:
|
community.general.redis_info:
|
||||||
login_port: "{{ slave_port }}"
|
login_port: "{{ replica_port }}"
|
||||||
login_password: "{{ redis_password }}"
|
login_password: "{{ redis_password }}"
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
|
@ -43,5 +43,5 @@
|
||||||
that:
|
that:
|
||||||
- result is not changed
|
- result is not changed
|
||||||
- result.info is defined
|
- result.info is defined
|
||||||
- result.info.tcp_port == slave_port
|
- result.info.tcp_port == replica_port
|
||||||
- result.info.role == 'slave'
|
- result.info.role == 'slave'
|
||||||
|
|
|
@ -22,14 +22,21 @@ redis_module: "{{ (ansible_python_version is version('2.7', '>=')) | ternary('re
|
||||||
|
|
||||||
redis_password: PASS
|
redis_password: PASS
|
||||||
|
|
||||||
|
old_redis: >-
|
||||||
|
{{
|
||||||
|
(ansible_distribution == 'CentOS' and ansible_distribution_major_version|int <= 7) or
|
||||||
|
(ansible_distribution == 'Ubuntu' and ansible_distribution_major_version|int <= 18) or
|
||||||
|
(ansible_os_family == 'FreeBSD' and ansible_distribution_major_version|int <= 12)
|
||||||
|
}}
|
||||||
|
|
||||||
# Master
|
# Master
|
||||||
master_port: 6379
|
master_port: 6379
|
||||||
master_conf: /etc/redis-master.conf
|
master_conf: /etc/redis-master.conf
|
||||||
master_datadir: /var/lib/redis-master
|
master_datadir: /var/lib/redis-master
|
||||||
master_logdir: /var/log/redis-master
|
master_logdir: /var/log/redis-master
|
||||||
|
|
||||||
# Slave
|
# Replica
|
||||||
slave_port: 6380
|
replica_port: 6380
|
||||||
slave_conf: /etc/redis-slave.conf
|
replica_conf: /etc/redis-replica.conf
|
||||||
slave_datadir: /var/lib/redis-slave
|
replica_datadir: /var/lib/redis-replica
|
||||||
slave_logdir: /var/log/redis-slave
|
replica_logdir: /var/log/redis-replica
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
- name: stop redis services
|
- name: stop redis services
|
||||||
shell: |
|
shell: |
|
||||||
kill -TERM $(cat /var/run/redis_{{ master_port }}.pid)
|
kill -TERM $(cat /var/run/redis_{{ master_port }}.pid)
|
||||||
kill -TERM $(cat /var/run/redis_{{ slave_port }}.pid)
|
kill -TERM $(cat /var/run/redis_{{ replica_port }}.pid)
|
||||||
listen: cleanup redis
|
listen: cleanup redis
|
||||||
|
|
||||||
- name: remove redis packages
|
- name: remove redis packages
|
||||||
|
@ -27,8 +27,8 @@
|
||||||
- "{{ master_datadir }}"
|
- "{{ master_datadir }}"
|
||||||
- "{{ master_logdir }}"
|
- "{{ master_logdir }}"
|
||||||
- /var/run/redis_{{ master_port }}.pid
|
- /var/run/redis_{{ master_port }}.pid
|
||||||
- "{{ slave_conf }}"
|
- "{{ replica_conf }}"
|
||||||
- "{{ slave_datadir }}"
|
- "{{ replica_datadir }}"
|
||||||
- "{{ slave_logdir }}"
|
- "{{ replica_logdir }}"
|
||||||
- /var/run/redis_{{ slave_port }}.pid
|
- /var/run/redis_{{ replica_port }}.pid
|
||||||
listen: cleanup redis
|
listen: cleanup redis
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# We run two servers listening different ports
|
# We run two servers listening different ports
|
||||||
# to be able to check replication (one server for master, another for slave).
|
# to be able to check replication (one server for master, another for replica).
|
||||||
|
|
||||||
- name: Install redis server apt dependencies
|
- name: Install redis server apt dependencies
|
||||||
apt:
|
apt:
|
||||||
|
@ -56,8 +56,8 @@
|
||||||
loop:
|
loop:
|
||||||
- "{{ master_datadir }}"
|
- "{{ master_datadir }}"
|
||||||
- "{{ master_logdir }}"
|
- "{{ master_logdir }}"
|
||||||
- "{{ slave_datadir }}"
|
- "{{ replica_datadir }}"
|
||||||
- "{{ slave_logdir }}"
|
- "{{ replica_logdir }}"
|
||||||
|
|
||||||
- name: Create redis configs
|
- name: Create redis configs
|
||||||
copy:
|
copy:
|
||||||
|
@ -75,16 +75,16 @@
|
||||||
port: "{{ master_port }}"
|
port: "{{ master_port }}"
|
||||||
logdir: "{{ master_logdir }}"
|
logdir: "{{ master_logdir }}"
|
||||||
datadir: "{{ master_datadir }}"
|
datadir: "{{ master_datadir }}"
|
||||||
- file: "{{ slave_conf }}"
|
- file: "{{ replica_conf }}"
|
||||||
port: "{{ slave_port }}"
|
port: "{{ replica_port }}"
|
||||||
logdir: "{{ slave_logdir }}"
|
logdir: "{{ replica_logdir }}"
|
||||||
datadir: "{{ slave_datadir }}"
|
datadir: "{{ replica_datadir }}"
|
||||||
|
|
||||||
- name: Start redis master
|
- name: Start redis master
|
||||||
shell: "{{ redis_bin[ansible_distribution] }} {{ master_conf }}"
|
shell: "{{ redis_bin[ansible_distribution] }} {{ master_conf }}"
|
||||||
|
|
||||||
- name: Start redis slave
|
- name: Start redis replica
|
||||||
shell: "{{ redis_bin[ansible_distribution] }} {{ slave_conf }} --slaveof 127.0.0.1 {{ master_port }}"
|
shell: "{{ redis_bin[ansible_distribution] }} {{ replica_conf }} --{% if old_redis %}slaveof{% else %}replicaof{% endif %} 127.0.0.1 {{ master_port }}"
|
||||||
|
|
||||||
- name: Wait for redis master to be started
|
- name: Wait for redis master to be started
|
||||||
ansible.builtin.wait_for:
|
ansible.builtin.wait_for:
|
||||||
|
@ -95,10 +95,10 @@
|
||||||
connect_timeout: 5
|
connect_timeout: 5
|
||||||
timeout: 30
|
timeout: 30
|
||||||
|
|
||||||
- name: Wait for redis slave to be started
|
- name: Wait for redis replica to be started
|
||||||
ansible.builtin.wait_for:
|
ansible.builtin.wait_for:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: "{{ slave_port }}"
|
port: "{{ replica_port }}"
|
||||||
state: started
|
state: started
|
||||||
delay: 1
|
delay: 1
|
||||||
connect_timeout: 5
|
connect_timeout: 5
|
||||||
|
|
Loading…
Reference in a new issue