Skip to content

Commit

Permalink
feat[redis cache plugin]: Add Redis connection parameters full contro…
Browse files Browse the repository at this point in the history
…l support
  • Loading branch information
pierre-claranet committed Jul 3, 2024
1 parent 01d8c7b commit e61d861
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions plugins/cache/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
options:
_uri:
description:
- A colon separated string of connection information for Redis.
- 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.
- Simple format:
- A colon separated string of connection information for Redis.
- 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.
- Full connection control format:
- Redis socket connection parameters as <name>=<value> pairs '&' separated, for example V(host=localhost&port=6379&db=0&password=changeme&socket_keepalive=true)
- See https://redis-py2.readthedocs.io/en/latest/ for Redis connection parameters name and accepted values
required: true
env:
- name: ANSIBLE_CACHE_PLUGIN_CONNECTION
Expand Down Expand Up @@ -94,6 +98,7 @@ class CacheModule(BaseCacheModule):
_sentinel_service_name = None
re_url_conn = re.compile(r'^([^:]+|\[[^]]+\]):(\d+):(\d+)(?::(.*))?$')
re_sent_conn = re.compile(r'^(.*):(\d+)$')
re_full_desc_url_conn = re.compile(r'^(?:([a-z_]+=[^&]+)&)(?:([a-z_]+=[^&]+)&)*([a-z_]+=[^&]+)$')

def __init__(self, *args, **kwargs):
uri = ''
Expand All @@ -112,19 +117,30 @@ def __init__(self, *args, **kwargs):
self._cache = {}
kw = {}

# tls connection
tlsprefix = 'tls://'
if uri.startswith(tlsprefix):
kw['ssl'] = True
uri = uri[len(tlsprefix):]
if self.re_full_desc_url_conn.match(uri):
conn_params = uri.split('&')

# redis sentinel connection
if self._sentinel_service_name:
self._db = self._get_sentinel_connection(uri, kw)
# normal connection
connection = {}

for param in conn_params:
p_key, p_value = param.split('=')
connection[p_key] = p_value

self._db = StrictRedis(**connection)
else:
connection = self._parse_connection(self.re_url_conn, uri)
self._db = StrictRedis(*connection, **kw)
# tls connection
tlsprefix = 'tls://'
if uri.startswith(tlsprefix):
kw['ssl'] = True
uri = uri[len(tlsprefix):]

# redis sentinel connection
if self._sentinel_service_name:
self._db = self._get_sentinel_connection(uri, kw)
# normal connection
else:
connection = self._parse_connection(self.re_url_conn, uri)
self._db = StrictRedis(*connection, **kw)

display.vv('Redis connection: %s' % self._db)

Expand Down

0 comments on commit e61d861

Please sign in to comment.