2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2014, Brian Coca, Josh Drake, et al
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
2022-08-05 12:28:29 +02:00
|
|
|
# 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 = '''
|
2020-09-28 21:21:51 +02:00
|
|
|
author: Unknown (!UNKNOWN)
|
2021-01-12 07:12:03 +01:00
|
|
|
name: redis
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: Use Redis DB for cache
|
|
|
|
description:
|
|
|
|
- This cache uses JSON formatted, per host records saved in Redis.
|
|
|
|
requirements:
|
|
|
|
- redis>=2.4.5 (python lib)
|
|
|
|
options:
|
|
|
|
_uri:
|
|
|
|
description:
|
|
|
|
- A colon separated string of connection information for Redis.
|
2023-06-10 10:53:50 +02:00
|
|
|
- The format is V(host:port:db:password), for example V(localhost:6379:0:changeme).
|
|
|
|
- To use encryption in transit, prefix the connection with V(tls://), as in V(tls://localhost:6379:0:changeme).
|
|
|
|
- To use redis sentinel, use separator V(;), for example V(localhost:26379;localhost:26379;0:changeme). Requires redis>=2.9.0.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CACHE_PLUGIN_CONNECTION
|
|
|
|
ini:
|
|
|
|
- key: fact_caching_connection
|
|
|
|
section: defaults
|
|
|
|
_prefix:
|
|
|
|
description: User defined prefix to use when creating the DB entries
|
|
|
|
default: ansible_facts
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CACHE_PLUGIN_PREFIX
|
|
|
|
ini:
|
|
|
|
- key: fact_caching_prefix
|
|
|
|
section: defaults
|
2020-10-11 15:16:59 +02:00
|
|
|
_keyset_name:
|
|
|
|
description: User defined name for cache keyset name.
|
|
|
|
default: ansible_cache_keys
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CACHE_REDIS_KEYSET_NAME
|
|
|
|
ini:
|
|
|
|
- key: fact_caching_redis_keyset_name
|
|
|
|
section: defaults
|
|
|
|
version_added: 1.3.0
|
2020-10-20 07:05:40 +02:00
|
|
|
_sentinel_service_name:
|
|
|
|
description: The redis sentinel service name (or referenced as cluster name).
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CACHE_REDIS_SENTINEL
|
|
|
|
ini:
|
|
|
|
- key: fact_caching_redis_sentinel
|
|
|
|
section: defaults
|
|
|
|
version_added: 1.3.0
|
2020-03-09 10:11:07 +01:00
|
|
|
_timeout:
|
|
|
|
default: 86400
|
|
|
|
description: Expiration timeout in seconds for the cache plugin data. Set to 0 to never expire
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CACHE_PLUGIN_TIMEOUT
|
|
|
|
ini:
|
|
|
|
- key: fact_caching_timeout
|
|
|
|
section: defaults
|
|
|
|
type: integer
|
|
|
|
'''
|
|
|
|
|
2021-05-26 07:07:09 +02:00
|
|
|
import re
|
2020-03-09 10:11:07 +01:00
|
|
|
import time
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.parsing.ajson import AnsibleJSONEncoder, AnsibleJSONDecoder
|
|
|
|
from ansible.plugins.cache import BaseCacheModule
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
|
|
|
|
try:
|
|
|
|
from redis import StrictRedis, VERSION
|
2020-11-13 21:34:56 +01:00
|
|
|
HAS_REDIS = True
|
2020-03-09 10:11:07 +01:00
|
|
|
except ImportError:
|
2020-11-13 21:34:56 +01:00
|
|
|
HAS_REDIS = False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
|
|
|
class CacheModule(BaseCacheModule):
|
|
|
|
"""
|
|
|
|
A caching module backed by redis.
|
|
|
|
|
|
|
|
Keys are maintained in a zset with their score being the timestamp
|
|
|
|
when they are inserted. This allows for the usage of 'zremrangebyscore'
|
|
|
|
to expire keys. This mechanism is used or a pattern matched 'scan' for
|
|
|
|
performance.
|
|
|
|
"""
|
2020-10-20 07:05:40 +02:00
|
|
|
_sentinel_service_name = None
|
2021-05-26 07:07:09 +02:00
|
|
|
re_url_conn = re.compile(r'^([^:]+|\[[^]]+\]):(\d+):(\d+)(?::(.*))?$')
|
|
|
|
re_sent_conn = re.compile(r'^(.*):(\d+)$')
|
2020-10-20 07:05:40 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
2020-05-30 12:26:37 +02:00
|
|
|
uri = ''
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-04-26 11:51:01 +02:00
|
|
|
super(CacheModule, self).__init__(*args, **kwargs)
|
|
|
|
if self.get_option('_uri'):
|
|
|
|
uri = self.get_option('_uri')
|
|
|
|
self._timeout = float(self.get_option('_timeout'))
|
|
|
|
self._prefix = self.get_option('_prefix')
|
|
|
|
self._keys_set = self.get_option('_keyset_name')
|
|
|
|
self._sentinel_service_name = self.get_option('_sentinel_service_name')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-11-13 21:34:56 +01:00
|
|
|
if not HAS_REDIS:
|
|
|
|
raise AnsibleError("The 'redis' python module (version 2.4.5 or newer) is required for the redis fact cache, 'pip install redis'")
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
self._cache = {}
|
2020-05-30 12:26:37 +02:00
|
|
|
kw = {}
|
2020-10-20 07:05:40 +02:00
|
|
|
|
|
|
|
# tls connection
|
2020-05-30 12:26:37 +02:00
|
|
|
tlsprefix = 'tls://'
|
|
|
|
if uri.startswith(tlsprefix):
|
|
|
|
kw['ssl'] = True
|
|
|
|
uri = uri[len(tlsprefix):]
|
|
|
|
|
2020-10-20 07:05:40 +02:00
|
|
|
# redis sentinel connection
|
|
|
|
if self._sentinel_service_name:
|
|
|
|
self._db = self._get_sentinel_connection(uri, kw)
|
|
|
|
# normal connection
|
|
|
|
else:
|
2021-05-26 07:07:09 +02:00
|
|
|
connection = self._parse_connection(self.re_url_conn, uri)
|
2020-10-20 07:05:40 +02:00
|
|
|
self._db = StrictRedis(*connection, **kw)
|
|
|
|
|
|
|
|
display.vv('Redis connection: %s' % self._db)
|
|
|
|
|
2021-05-26 07:07:09 +02:00
|
|
|
@staticmethod
|
|
|
|
def _parse_connection(re_patt, uri):
|
|
|
|
match = re_patt.match(uri)
|
|
|
|
if not match:
|
|
|
|
raise AnsibleError("Unable to parse connection string")
|
|
|
|
return match.groups()
|
|
|
|
|
2020-10-20 07:05:40 +02:00
|
|
|
def _get_sentinel_connection(self, uri, kw):
|
|
|
|
"""
|
|
|
|
get sentinel connection details from _uri
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
from redis.sentinel import Sentinel
|
|
|
|
except ImportError:
|
|
|
|
raise AnsibleError("The 'redis' python module (version 2.9.0 or newer) is required to use redis sentinel.")
|
|
|
|
|
|
|
|
if ';' not in uri:
|
|
|
|
raise AnsibleError('_uri does not have sentinel syntax.')
|
|
|
|
|
|
|
|
# format: "localhost:26379;localhost2:26379;0:changeme"
|
|
|
|
connections = uri.split(';')
|
|
|
|
connection_args = connections.pop(-1)
|
2023-10-29 20:26:39 +01:00
|
|
|
if len(connection_args) > 0: # handle if no db nr is given
|
2020-10-20 07:05:40 +02:00
|
|
|
connection_args = connection_args.split(':')
|
|
|
|
kw['db'] = connection_args.pop(0)
|
|
|
|
try:
|
|
|
|
kw['password'] = connection_args.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
pass # password is optional
|
|
|
|
|
2021-05-26 07:07:09 +02:00
|
|
|
sentinels = [self._parse_connection(self.re_sent_conn, shost) for shost in connections]
|
2020-10-20 07:05:40 +02:00
|
|
|
display.vv('\nUsing redis sentinels: %s' % sentinels)
|
|
|
|
scon = Sentinel(sentinels, **kw)
|
|
|
|
try:
|
|
|
|
return scon.master_for(self._sentinel_service_name, socket_timeout=0.2)
|
|
|
|
except Exception as exc:
|
|
|
|
raise AnsibleError('Could not connect to redis sentinel: %s' % to_native(exc))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def _make_key(self, key):
|
|
|
|
return self._prefix + key
|
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
|
|
|
|
if key not in self._cache:
|
|
|
|
value = self._db.get(self._make_key(key))
|
|
|
|
# guard against the key not being removed from the zset;
|
|
|
|
# this could happen in cases where the timeout value is changed
|
|
|
|
# between invocations
|
|
|
|
if value is None:
|
|
|
|
self.delete(key)
|
|
|
|
raise KeyError
|
|
|
|
self._cache[key] = json.loads(value, cls=AnsibleJSONDecoder)
|
|
|
|
|
|
|
|
return self._cache.get(key)
|
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
|
|
|
|
value2 = json.dumps(value, cls=AnsibleJSONEncoder, sort_keys=True, indent=4)
|
|
|
|
if self._timeout > 0: # a timeout of 0 is handled as meaning 'never expire'
|
|
|
|
self._db.setex(self._make_key(key), int(self._timeout), value2)
|
|
|
|
else:
|
|
|
|
self._db.set(self._make_key(key), value2)
|
|
|
|
|
|
|
|
if VERSION[0] == 2:
|
|
|
|
self._db.zadd(self._keys_set, time.time(), key)
|
|
|
|
else:
|
|
|
|
self._db.zadd(self._keys_set, {key: time.time()})
|
|
|
|
self._cache[key] = value
|
|
|
|
|
|
|
|
def _expire_keys(self):
|
|
|
|
if self._timeout > 0:
|
|
|
|
expiry_age = time.time() - self._timeout
|
|
|
|
self._db.zremrangebyscore(self._keys_set, 0, expiry_age)
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
self._expire_keys()
|
|
|
|
return self._db.zrange(self._keys_set, 0, -1)
|
|
|
|
|
|
|
|
def contains(self, key):
|
|
|
|
self._expire_keys()
|
|
|
|
return (self._db.zrank(self._keys_set, key) is not None)
|
|
|
|
|
|
|
|
def delete(self, key):
|
|
|
|
if key in self._cache:
|
|
|
|
del self._cache[key]
|
|
|
|
self._db.delete(self._make_key(key))
|
|
|
|
self._db.zrem(self._keys_set, key)
|
|
|
|
|
|
|
|
def flush(self):
|
2021-02-23 23:19:27 +01:00
|
|
|
for key in list(self.keys()):
|
2020-03-09 10:11:07 +01:00
|
|
|
self.delete(key)
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
# TODO: there is probably a better way to do this in redis
|
2021-02-23 23:19:27 +01:00
|
|
|
ret = dict([(k, self.get(k)) for k in self.keys()])
|
2020-03-09 10:11:07 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
return dict()
|
|
|
|
|
|
|
|
def __setstate__(self, data):
|
|
|
|
self.__init__()
|