2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: redis
|
2021-06-24 22:33:29 +02:00
|
|
|
short_description: Various redis commands, replica and flush
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Unified utility to interact with redis instances.
|
2022-03-11 06:54:38 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.redis
|
2023-02-24 09:23:04 +01:00
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
command:
|
|
|
|
description:
|
|
|
|
- The selected redis command
|
2023-06-20 11:08:32 +02:00
|
|
|
- V(config) ensures a configuration setting on an instance.
|
|
|
|
- V(flush) flushes all the instance or a specified db.
|
|
|
|
- V(replica) sets a redis instance in replica or master mode. (V(slave) is an alias for V(replica).)
|
2021-06-24 22:33:29 +02:00
|
|
|
choices: [ config, flush, replica, slave ]
|
2020-11-28 14:06:28 +01:00
|
|
|
type: str
|
2022-03-11 06:54:38 +01:00
|
|
|
tls:
|
|
|
|
default: false
|
|
|
|
version_added: 4.6.0
|
|
|
|
login_user:
|
|
|
|
version_added: 4.6.0
|
|
|
|
validate_certs:
|
|
|
|
version_added: 4.6.0
|
|
|
|
ca_certs:
|
|
|
|
version_added: 4.6.0
|
2020-03-09 10:11:07 +01:00
|
|
|
master_host:
|
|
|
|
description:
|
2021-06-24 22:33:29 +02:00
|
|
|
- The host of the master instance [replica command]
|
2020-11-28 14:06:28 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
master_port:
|
|
|
|
description:
|
2021-06-24 22:33:29 +02:00
|
|
|
- The port of the master instance [replica command]
|
2020-11-28 14:06:28 +01:00
|
|
|
type: int
|
2021-06-24 22:33:29 +02:00
|
|
|
replica_mode:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-06-24 22:33:29 +02:00
|
|
|
- The mode of the redis instance [replica command]
|
2023-06-20 11:08:32 +02:00
|
|
|
- V(slave) is an alias for V(replica).
|
2021-06-24 22:33:29 +02:00
|
|
|
default: replica
|
|
|
|
choices: [ master, replica, slave ]
|
2020-11-28 14:06:28 +01:00
|
|
|
type: str
|
2021-06-24 22:33:29 +02:00
|
|
|
aliases:
|
|
|
|
- slave_mode
|
2020-03-09 10:11:07 +01:00
|
|
|
db:
|
|
|
|
description:
|
|
|
|
- The database to flush (used in db mode) [flush command]
|
2020-11-28 14:06:28 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
flush_mode:
|
|
|
|
description:
|
|
|
|
- Type of flush (all the dbs in a redis instance or a specific one)
|
|
|
|
[flush command]
|
|
|
|
default: all
|
|
|
|
choices: [ all, db ]
|
2020-11-28 14:06:28 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- A redis config key.
|
2020-11-28 14:06:28 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- A redis config value. When memory size is needed, it is possible
|
2023-10-26 06:20:21 +02:00
|
|
|
to specify it in the usual form of 1KB, 2M, 400MB where the base is 1024.
|
2020-03-09 10:11:07 +01:00
|
|
|
Units are case insensitive i.e. 1m = 1mb = 1M = 1MB.
|
2020-11-28 14:06:28 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
notes:
|
|
|
|
- Requires the redis-py Python package on the remote host. You can
|
|
|
|
install it with pip (pip install redis) or with a package manager.
|
|
|
|
https://github.com/andymccurdy/redis-py
|
2021-06-24 22:33:29 +02:00
|
|
|
- If the redis master instance we are making replica of is password protected
|
2020-03-09 10:11:07 +01:00
|
|
|
this needs to be in the redis.conf in the masterauth variable
|
|
|
|
|
2020-05-12 19:18:24 +02:00
|
|
|
seealso:
|
2020-06-25 13:34:46 +02:00
|
|
|
- module: community.general.redis_info
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements: [ redis ]
|
|
|
|
author: "Xabier Larrakoetxea (@slok)"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2021-06-24 22:33:29 +02:00
|
|
|
- name: Set local redis instance to be a replica of melee.island on port 6377
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2021-06-24 22:33:29 +02:00
|
|
|
command: replica
|
2020-03-09 10:11:07 +01:00
|
|
|
master_host: melee.island
|
|
|
|
master_port: 6377
|
|
|
|
|
2021-06-24 22:33:29 +02:00
|
|
|
- name: Deactivate replica mode
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2021-06-24 22:33:29 +02:00
|
|
|
command: replica
|
|
|
|
replica_mode: master
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Flush all the redis db
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: flush
|
|
|
|
flush_mode: all
|
|
|
|
|
|
|
|
- name: Flush only one db in a redis instance
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: flush
|
|
|
|
db: 1
|
|
|
|
flush_mode: db
|
|
|
|
|
|
|
|
- name: Configure local redis to have 10000 max clients
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: config
|
|
|
|
name: maxclients
|
|
|
|
value: 10000
|
|
|
|
|
|
|
|
- name: Configure local redis maxmemory to 4GB
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: config
|
|
|
|
name: maxmemory
|
|
|
|
value: 4GB
|
|
|
|
|
|
|
|
- name: Configure local redis to have lua time limit of 100 ms
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.redis:
|
2020-03-09 10:11:07 +01:00
|
|
|
command: config
|
|
|
|
name: lua-time-limit
|
|
|
|
value: 100
|
|
|
|
'''
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
REDIS_IMP_ERR = None
|
|
|
|
try:
|
|
|
|
import redis
|
|
|
|
except ImportError:
|
|
|
|
REDIS_IMP_ERR = traceback.format_exc()
|
|
|
|
redis_found = False
|
|
|
|
else:
|
|
|
|
redis_found = True
|
|
|
|
|
2022-09-09 22:23:48 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.common.text.formatters import human_to_bytes
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2022-03-11 06:54:38 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.redis import (
|
|
|
|
fail_imports, redis_auth_argument_spec, redis_auth_params)
|
2020-10-20 07:04:42 +02:00
|
|
|
import re
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Redis module specific support methods.
|
2021-06-24 22:33:29 +02:00
|
|
|
def set_replica_mode(client, master_host, master_port):
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
return client.slaveof(master_host, master_port)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def set_master_mode(client):
|
|
|
|
try:
|
|
|
|
return client.slaveof()
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def flush(client, db=None):
|
|
|
|
try:
|
|
|
|
if not isinstance(db, int):
|
|
|
|
return client.flushall()
|
|
|
|
else:
|
|
|
|
# The passed client has been connected to the database already
|
|
|
|
return client.flushdb()
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# Module execution.
|
|
|
|
def main():
|
2022-03-11 06:54:38 +01:00
|
|
|
redis_auth_args = redis_auth_argument_spec(tls_default=False)
|
|
|
|
module_args = dict(
|
|
|
|
command=dict(type='str', choices=['config', 'flush', 'replica', 'slave']),
|
|
|
|
master_host=dict(type='str'),
|
|
|
|
master_port=dict(type='int'),
|
|
|
|
replica_mode=dict(type='str', default='replica', choices=['master', 'replica', 'slave'],
|
|
|
|
aliases=["slave_mode"]),
|
|
|
|
db=dict(type='int'),
|
|
|
|
flush_mode=dict(type='str', default='all', choices=['all', 'db']),
|
|
|
|
name=dict(type='str'),
|
|
|
|
value=dict(type='str'),
|
|
|
|
)
|
|
|
|
module_args.update(redis_auth_args)
|
2020-03-09 10:11:07 +01:00
|
|
|
module = AnsibleModule(
|
2022-03-11 06:54:38 +01:00
|
|
|
argument_spec=module_args,
|
2020-03-09 10:11:07 +01:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2022-03-11 06:54:38 +01:00
|
|
|
fail_imports(module, module.params['tls'])
|
|
|
|
|
|
|
|
redis_params = redis_auth_params(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
command = module.params['command']
|
|
|
|
if command == "slave":
|
2021-06-24 22:33:29 +02:00
|
|
|
command = "replica"
|
|
|
|
|
|
|
|
# Replica Command section -----------
|
|
|
|
if command == "replica":
|
2020-03-09 10:11:07 +01:00
|
|
|
master_host = module.params['master_host']
|
|
|
|
master_port = module.params['master_port']
|
2021-06-24 22:33:29 +02:00
|
|
|
mode = module.params['replica_mode']
|
|
|
|
if mode == "slave":
|
|
|
|
mode = "replica"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Check if we have all the data
|
2021-06-24 22:33:29 +02:00
|
|
|
if mode == "replica": # Only need data if we want to be replica
|
2020-03-09 10:11:07 +01:00
|
|
|
if not master_host:
|
2021-06-24 22:33:29 +02:00
|
|
|
module.fail_json(msg='In replica mode master host must be provided')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not master_port:
|
2021-06-24 22:33:29 +02:00
|
|
|
module.fail_json(msg='In replica mode master port must be provided')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Connect and check
|
2022-03-11 06:54:38 +01:00
|
|
|
r = redis.StrictRedis(**redis_params)
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
r.ping()
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
# Check if we are already in the mode that we want
|
|
|
|
info = r.info()
|
|
|
|
if mode == "master" and info["role"] == "master":
|
|
|
|
module.exit_json(changed=False, mode=mode)
|
|
|
|
|
2021-06-24 22:33:29 +02:00
|
|
|
elif mode == "replica" and info["role"] == "slave" and info["master_host"] == master_host and info["master_port"] == master_port:
|
2020-03-09 10:11:07 +01:00
|
|
|
status = dict(
|
|
|
|
status=mode,
|
|
|
|
master_host=master_host,
|
|
|
|
master_port=master_port,
|
|
|
|
)
|
|
|
|
module.exit_json(changed=False, mode=status)
|
|
|
|
else:
|
|
|
|
# Do the stuff
|
|
|
|
# (Check Check_mode before commands so the commands aren't evaluated
|
|
|
|
# if not necessary)
|
2021-06-24 22:33:29 +02:00
|
|
|
if mode == "replica":
|
|
|
|
if module.check_mode or set_replica_mode(r, master_host, master_port):
|
2020-03-09 10:11:07 +01:00
|
|
|
info = r.info()
|
|
|
|
status = {
|
|
|
|
'status': mode,
|
|
|
|
'master_host': master_host,
|
|
|
|
'master_port': master_port,
|
|
|
|
}
|
|
|
|
module.exit_json(changed=True, mode=status)
|
|
|
|
else:
|
2021-06-24 22:33:29 +02:00
|
|
|
module.fail_json(msg='Unable to set replica mode')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
if module.check_mode or set_master_mode(r):
|
|
|
|
module.exit_json(changed=True, mode=mode)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='Unable to set master mode')
|
|
|
|
|
|
|
|
# flush Command section -----------
|
|
|
|
elif command == "flush":
|
|
|
|
db = module.params['db']
|
|
|
|
mode = module.params['flush_mode']
|
|
|
|
|
|
|
|
# Check if we have all the data
|
|
|
|
if mode == "db":
|
|
|
|
if db is None:
|
|
|
|
module.fail_json(msg="In db mode the db number must be provided")
|
|
|
|
|
|
|
|
# Connect and check
|
2022-03-11 06:54:38 +01:00
|
|
|
r = redis.StrictRedis(db=db, **redis_params)
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
r.ping()
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
# Do the stuff
|
|
|
|
# (Check Check_mode before commands so the commands aren't evaluated
|
|
|
|
# if not necessary)
|
|
|
|
if mode == "all":
|
|
|
|
if module.check_mode or flush(r):
|
|
|
|
module.exit_json(changed=True, flushed=True)
|
|
|
|
else: # Flush never fails :)
|
|
|
|
module.fail_json(msg="Unable to flush all databases")
|
|
|
|
|
|
|
|
else:
|
|
|
|
if module.check_mode or flush(r, db):
|
|
|
|
module.exit_json(changed=True, flushed=True, db=db)
|
|
|
|
else: # Flush never fails :)
|
|
|
|
module.fail_json(msg="Unable to flush '%d' database" % db)
|
|
|
|
elif command == 'config':
|
|
|
|
name = module.params['name']
|
|
|
|
|
|
|
|
try: # try to parse the value as if it were the memory size
|
2020-10-20 07:04:42 +02:00
|
|
|
if re.match(r'^\s*(\d*\.?\d*)\s*([A-Za-z]+)?\s*$', module.params['value'].upper()):
|
|
|
|
value = str(human_to_bytes(module.params['value'].upper()))
|
|
|
|
else:
|
|
|
|
value = module.params['value']
|
2020-03-09 10:11:07 +01:00
|
|
|
except ValueError:
|
|
|
|
value = module.params['value']
|
|
|
|
|
2022-03-11 06:54:38 +01:00
|
|
|
r = redis.StrictRedis(**redis_params)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
r.ping()
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
try:
|
|
|
|
old_value = r.config_get(name)[name]
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to read config: %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
changed = old_value != value
|
|
|
|
|
|
|
|
if module.check_mode or not changed:
|
|
|
|
module.exit_json(changed=changed, name=name, value=value)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
r.config_set(name, value)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="unable to write config: %s" % to_native(e), exception=traceback.format_exc())
|
|
|
|
module.exit_json(changed=changed, name=name, value=value)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='A valid command must be provided')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|