Debian Codegraph @

ansible_collections/community/general/plugins/modules/redis_data_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2021, Andreas Botzner <andreas at botzner dot com>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: redis_data_info
11 short_description: Get value of key in Redis database
12 version_added: 3.7.0
13 description:
14 - Get value of keys in Redis database.
15 author: "Andreas Botzner (@paginabianca)"
16 options:
17 key:
18 description:
19 - Database key.
20 type: str
21 required: true
22
23 extends_documentation_fragment:
24 - community.general._redis
25 - community.general._attributes
26 - community.general._attributes.info_module
27
28 seealso:
29 - module: community.general.redis_data
30 - module: community.general.redis_data_incr
31 - module: community.general.redis_info
32 - module: community.general.redis
33 """
34
35 EXAMPLES = r"""
36 - name: Get key foo=bar from loalhost with no username
37 community.general.redis_data_info:
38 login_host: localhost
39 login_password: supersecret
40 key: foo
41
42 - name: Get key foo=bar on redishost with custom ca-cert file
43 community.general.redis_data_info:
44 login_host: redishost
45 login_password: supersecret
46 login_user: somuser
47 validate_certs: true
48 ssl_ca_certs: /path/to/ca/certs
49 key: foo
50 """
51
52 RETURN = r"""
53 exists:
54 description: If they key exists in the database.
55 returned: on success
56 type: bool
57 value:
58 description: Value key was set to.
59 returned: if existing
60 type: str
61 sample: 'value_of_some_key'
62 msg:
63 description: A short message.
64 returned: always
65 type: str
66 sample: 'Got key: foo with value: bar'
67 """
68
69 from ansible.module_utils.basic import AnsibleModule
70
71 from ansible_collections.community.general.plugins.module_utils._redis import (
72 RedisAnsible,
73 fail_imports,
74 redis_auth_argument_spec,
75 )
76
77
78 def main():
79 redis_auth_args = redis_auth_argument_spec()
80 module_args = dict(
81 key=dict(type="str", required=True, no_log=False),
82 )
83 module_args.update(redis_auth_args)
84
85 module = AnsibleModule(
86 argument_spec=module_args,
87 supports_check_mode=True,
88 )
89 fail_imports(module)
90
91 redis = RedisAnsible(module)
92
93 key = module.params["key"]
94 result = {"changed": False}
95
96 value = None
97 try:
98 value = redis.connection.get(key)
99 except Exception as e:
100 msg = f'Failed to get value of key "{key}" with exception: {e}'
101 result["msg"] = msg
102 module.fail_json(**result)
103
104 if value is None:
105 msg = f'Key "{key}" does not exist in database'
106 result["exists"] = False
107 else:
108 msg = f'Got key "{key}"'
109 result["value"] = value
110 result["exists"] = True
111 result["msg"] = msg
112 module.exit_json(**result)
113
114
115 if __name__ == "__main__":
116 main()