Skip to content

Commit

Permalink
feat(redis_info.py): use module_utils redis.py to support TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
greg5813 authored and Grégoire Martini committed Sep 15, 2023
1 parent e7ff0ac commit c4bc8ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7267-redis_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- redis_info - Refactor the redis_info module to use the redis module_utils enabling to pass TLS parameters to the Redis client.
38 changes: 21 additions & 17 deletions plugins/modules/redis_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
description:
- The password used to authenticate with, when authentication is enabled for the Redis server.
type: str
tls:
description:
- Establish a secure TLS connection.
type: bool
default: false
ca_certs:
description:
- CA Certificate file to verify with.
type: str
validate_certs:
description:
- Validate the SSL server certificate.
type: bool
default: true
notes:
- Requires the redis-py Python package on the remote host. You can
install it with pip (C(pip install redis)) or with a package manager.
Expand Down Expand Up @@ -199,34 +213,24 @@
REDIS_IMP_ERR = traceback.format_exc()
HAS_REDIS_PACKAGE = False

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native


def redis_client(**client_params):
return StrictRedis(**client_params)

from ansible_collections.community.general.plugins.module_utils.redis import (
fail_imports, redis_auth_argument_spec, redis_auth_params)

# Module execution.
def main():
module = AnsibleModule(
argument_spec=dict(
login_host=dict(type='str', default='localhost'),
login_port=dict(type='int', default=6379),
login_password=dict(type='str', no_log=True),
),
argument_spec=redis_auth_argument_spec(tls_default=False),
supports_check_mode=True,
)

if not HAS_REDIS_PACKAGE:
module.fail_json(msg=missing_required_lib('redis'), exception=REDIS_IMP_ERR)
fail_imports(module, module.params['tls'])

login_host = module.params['login_host']
login_port = module.params['login_port']
login_password = module.params['login_password']
redis_params = redis_auth_params(module)

# Connect and check
client = redis_client(host=login_host, port=login_port, password=login_password)
client = StrictRedis(**redis_params)
try:
client.ping()
except Exception as e:
Expand Down

0 comments on commit c4bc8ab

Please sign in to comment.