mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 02:41:25 -07:00
Adds redis_data_info module (#3227)
* Added redis_data_info module Added: - redis_data_info module and suggested 'exists' return flag. - module_utils for redis with a base class that handles database connections. - inhereited unit tests and added some new ones for the exit flag * Docfix and sanity * typo * Suggested doc changes and ssl option * TLS and validate_certs fix * Set support_check_mode for info plugin * Docfix and import errors * Redis versioning Fix * version bump and append fixes
This commit is contained in:
parent
dd25c0d3bf
commit
6b207bce4c
6 changed files with 377 additions and 0 deletions
111
plugins/modules/database/misc/redis_data_info.py
Normal file
111
plugins/modules/database/misc/redis_data_info.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright: (c) 2021, Andreas Botzner <andreas at botzner dot com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: redis_data_info
|
||||
short_description: Get value of key in Redis database
|
||||
version_added: 3.7.0
|
||||
description:
|
||||
- Get value of keys in Redis database.
|
||||
author: "Andreas Botzner (@paginabianca)"
|
||||
options:
|
||||
key:
|
||||
description:
|
||||
- Database key.
|
||||
type: str
|
||||
required: true
|
||||
|
||||
extends_documentation_fragment:
|
||||
- community.general.redis
|
||||
|
||||
seealso:
|
||||
- module: community.general.redis_info
|
||||
- module: community.general.redis
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Get key foo=bar from loalhost with no username
|
||||
community.general.redis_data_info:
|
||||
login_host: localhost
|
||||
login_password: supersecret
|
||||
key: foo
|
||||
|
||||
- name: Get key foo=bar on redishost with custom ca-cert file
|
||||
community.general.redis_data_info:
|
||||
login_host: redishost
|
||||
login_password: supersecret
|
||||
login_user: somuser
|
||||
validate_certs: true
|
||||
ssl_ca_certs: /path/to/ca/certs
|
||||
key: foo
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
exists:
|
||||
description: If they key exists in the database.
|
||||
returned: on success
|
||||
type: bool
|
||||
value:
|
||||
description: Value key was set to.
|
||||
returned: if existing
|
||||
type: str
|
||||
sample: 'value_of_some_key'
|
||||
msg:
|
||||
description: A short message.
|
||||
returned: always
|
||||
type: str
|
||||
sample: 'Got key: foo with value: bar'
|
||||
'''
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible_collections.community.general.plugins.module_utils.redis import (
|
||||
fail_imports, redis_auth_argument_spec, RedisAnsible)
|
||||
|
||||
|
||||
def main():
|
||||
redis_auth_args = redis_auth_argument_spec()
|
||||
module_args = dict(
|
||||
key=dict(type='str', required=True, no_log=False),
|
||||
)
|
||||
module_args.update(redis_auth_args)
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec=module_args,
|
||||
supports_check_mode=True,
|
||||
)
|
||||
fail_imports(module)
|
||||
|
||||
redis = RedisAnsible(module)
|
||||
|
||||
key = module.params['key']
|
||||
result = {'changed': False}
|
||||
|
||||
value = None
|
||||
try:
|
||||
value = redis.connection.get(key)
|
||||
except Exception as e:
|
||||
msg = 'Failed to get value of key "{0}" with exception: {1}'.format(
|
||||
key, str(e))
|
||||
result['msg'] = msg
|
||||
module.fail_json(**result)
|
||||
|
||||
if value is None:
|
||||
msg = 'Key "{0}" does not exist in database'.format(key)
|
||||
result['exists'] = False
|
||||
else:
|
||||
msg = 'Got key "{0}"'.format(key)
|
||||
result['value'] = value
|
||||
result['exists'] = True
|
||||
result['msg'] = msg
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue