summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/redis_data_info.py
blob: c0af619057d06226aaed6cfcd81860f28329021f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
# -*- coding: utf-8 -*-

# 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

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
  - community.general.attributes
  - community.general.attributes.info_module

seealso:
  - module: community.general.redis_data
  - module: community.general.redis_data_incr
  - 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()