2021-09-08 07:14:37 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2021, Andreas Botzner <andreas at botzner dot com>
|
|
|
|
# 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
|
2021-09-08 07:14:37 +02:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import missing_required_lib
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
REDIS_IMP_ERR = None
|
|
|
|
try:
|
|
|
|
from redis import Redis
|
|
|
|
from redis import __version__ as redis_version
|
|
|
|
HAS_REDIS_PACKAGE = True
|
2022-08-12 11:07:30 +02:00
|
|
|
REDIS_IMP_ERR = None
|
2021-09-08 07:14:37 +02:00
|
|
|
except ImportError:
|
|
|
|
REDIS_IMP_ERR = traceback.format_exc()
|
|
|
|
HAS_REDIS_PACKAGE = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
import certifi
|
|
|
|
HAS_CERTIFI_PACKAGE = True
|
2022-08-12 11:07:30 +02:00
|
|
|
CERTIFI_IMPORT_ERROR = None
|
2021-09-08 07:14:37 +02:00
|
|
|
except ImportError:
|
|
|
|
CERTIFI_IMPORT_ERROR = traceback.format_exc()
|
|
|
|
HAS_CERTIFI_PACKAGE = False
|
|
|
|
|
|
|
|
|
2022-03-11 06:54:38 +01:00
|
|
|
def fail_imports(module, needs_certifi=True):
|
2021-09-08 07:14:37 +02:00
|
|
|
errors = []
|
|
|
|
traceback = []
|
|
|
|
if not HAS_REDIS_PACKAGE:
|
|
|
|
errors.append(missing_required_lib('redis'))
|
|
|
|
traceback.append(REDIS_IMP_ERR)
|
2022-03-11 06:54:38 +01:00
|
|
|
if not HAS_CERTIFI_PACKAGE and needs_certifi:
|
2021-09-08 07:14:37 +02:00
|
|
|
errors.append(missing_required_lib('certifi'))
|
|
|
|
traceback.append(CERTIFI_IMPORT_ERROR)
|
|
|
|
if errors:
|
fix invalid fail_json call (#4733)
* fix invalid fail_json call
Currently causes this error:
```
TypeError: AnsibleModule.fail_json() missing 1 required positional argument: 'msg'
gitlab | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.10"
},
"changed": false,
"module_stderr": "Shared connection to 10.0.3.100 closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/root/.ansible/tmp/ansible-tmp-1653579059.8318024-147674-84188943153768/AnsiballZ_redis.py\", line 107, in <module>\r\n _ansiballz_main()\r\n File \"/root/.ansible/tmp/ansible-tmp-1653579059.8318024-147674-84188943153768/AnsiballZ_redis.py\", line 99, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/root/.ansible/tmp/ansible-tmp-1653579059.8318024-147674-84188943153768/AnsiballZ_redis.py\", line 47, in invoke_module\r\n runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.redis', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.redis', _modlib_path=modlib_path),\r\n File \"/usr/lib/python3.10/runpy.py\", line 209, in run_module\r\n return _run_module_code(code, init_globals, run_name, mod_spec)\r\n File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\r\n _run_code(code, mod_globals, init_globals,\r\n File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\r\n exec(code, run_globals)\r\n File \"/tmp/ansible_community.general.redis_payload_di15cy0s/ansible_community.general.redis_payload.zip/ansible_collections/community/general/plugins/modules/redis.py\", line 328, in <module>\r\n File \"/tmp/ansible_community.general.redis_payload_di15cy0s/ansible_community.general.redis_payload.zip/ansible_collections/community/general/plugins/modules/redis.py\", line 195, in main\r\n File \"/tmp/ansible_community.general.redis_payload_di15cy0s/ansible_community.general.redis_payload.zip/ansible_collections/community/general/plugins/module_utils/redis.py\", line 40, in fail_imports\r\nTypeError: AnsibleModule.fail_json() missing 1 required positional argument: 'msg'\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
```
* Add changelog.
Co-authored-by: Felix Fontein <felix@fontein.de>
2022-05-30 08:03:25 +02:00
|
|
|
module.fail_json(msg='\n'.join(errors), traceback='\n'.join(traceback))
|
2021-09-08 07:14:37 +02:00
|
|
|
|
|
|
|
|
2022-03-11 06:54:38 +01:00
|
|
|
def redis_auth_argument_spec(tls_default=True):
|
2021-09-08 07:14:37 +02:00
|
|
|
return dict(
|
|
|
|
login_host=dict(type='str',
|
|
|
|
default='localhost',),
|
|
|
|
login_user=dict(type='str'),
|
|
|
|
login_password=dict(type='str',
|
|
|
|
no_log=True
|
|
|
|
),
|
|
|
|
login_port=dict(type='int', default=6379),
|
|
|
|
tls=dict(type='bool',
|
2022-03-11 06:54:38 +01:00
|
|
|
default=tls_default),
|
2021-09-08 07:14:37 +02:00
|
|
|
validate_certs=dict(type='bool',
|
|
|
|
default=True
|
|
|
|
),
|
2024-07-23 22:03:13 +02:00
|
|
|
ca_certs=dict(type='str'),
|
|
|
|
client_cert_file=dict(type='str'),
|
|
|
|
client_key_file=dict(type='str'),
|
2021-09-08 07:14:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-11 06:54:38 +01:00
|
|
|
def redis_auth_params(module):
|
|
|
|
login_host = module.params['login_host']
|
|
|
|
login_user = module.params['login_user']
|
|
|
|
login_password = module.params['login_password']
|
|
|
|
login_port = module.params['login_port']
|
|
|
|
tls = module.params['tls']
|
|
|
|
validate_certs = 'required' if module.params['validate_certs'] else None
|
|
|
|
ca_certs = module.params['ca_certs']
|
|
|
|
if tls and ca_certs is None:
|
|
|
|
ca_certs = str(certifi.where())
|
2024-07-23 22:03:13 +02:00
|
|
|
client_cert_file = module.params['client_cert_file']
|
|
|
|
client_key_file = module.params['client_key_file']
|
2022-03-11 06:54:38 +01:00
|
|
|
if tuple(map(int, redis_version.split('.'))) < (3, 4, 0) and login_user is not None:
|
|
|
|
module.fail_json(
|
|
|
|
msg='The option `username` in only supported with redis >= 3.4.0.')
|
|
|
|
params = {'host': login_host,
|
|
|
|
'port': login_port,
|
|
|
|
'password': login_password,
|
|
|
|
'ssl_ca_certs': ca_certs,
|
2024-07-23 22:03:13 +02:00
|
|
|
'ssl_certfile': client_cert_file,
|
|
|
|
'ssl_keyfile': client_key_file,
|
2022-03-11 06:54:38 +01:00
|
|
|
'ssl_cert_reqs': validate_certs,
|
|
|
|
'ssl': tls}
|
|
|
|
if login_user is not None:
|
|
|
|
params['username'] = login_user
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
2021-09-08 07:14:37 +02:00
|
|
|
class RedisAnsible(object):
|
|
|
|
'''Base class for Redis module'''
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.connection = self._connect()
|
|
|
|
|
|
|
|
def _connect(self):
|
|
|
|
try:
|
2022-03-11 06:54:38 +01:00
|
|
|
return Redis(**redis_auth_params(self.module))
|
2021-09-08 07:14:37 +02:00
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg='{0}'.format(str(e)))
|
|
|
|
return None
|