| 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_incr |
| 11 |
short_description: Increment keys in Redis |
| 12 |
version_added: 4.0.0 |
| 13 |
description: |
| 14 |
- Increment integers or float keys in Redis database and get new value. |
| 15 |
- Default increment for all keys is V(1). For specific increments use the O(increment_int) and O(increment_float) options. |
| 16 |
author: "Andreas Botzner (@paginabianca)" |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: partial |
| 20 |
details: |
| 21 |
- For C(check_mode) to work, the specified O(login_user) needs permission to run the C(GET) command on the key, otherwise |
| 22 |
the module fails. |
| 23 |
- When using C(check_mode) the module tries to calculate the value that Redis would return. If the key is not present, |
| 24 |
V(0.0) is used as value. |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
key: |
| 29 |
description: |
| 30 |
- Database key. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
increment_int: |
| 34 |
description: |
| 35 |
- Integer amount to increment the key by. |
| 36 |
type: int |
| 37 |
increment_float: |
| 38 |
description: |
| 39 |
- Float amount to increment the key by. |
| 40 |
- This only works with keys that contain float values in their string representation. |
| 41 |
type: float |
| 42 |
|
| 43 |
|
| 44 |
extends_documentation_fragment: |
| 45 |
- community.general._redis.documentation |
| 46 |
- community.general._attributes |
| 47 |
|
| 48 |
seealso: |
| 49 |
- module: community.general.redis_data |
| 50 |
- module: community.general.redis_data_info |
| 51 |
- module: community.general.redis |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: Increment integer key foo on localhost with no username and print new value |
| 56 |
community.general.redis_data_incr: |
| 57 |
login_host: localhost |
| 58 |
login_password: supersecret |
| 59 |
key: foo |
| 60 |
increment_int: 1 |
| 61 |
register: result |
| 62 |
- name: Print new value |
| 63 |
debug: |
| 64 |
var: result.value |
| 65 |
|
| 66 |
- name: Increment float key foo by 20.4 |
| 67 |
community.general.redis_data_incr: |
| 68 |
login_host: redishost |
| 69 |
login_user: redisuser |
| 70 |
login_password: somepass |
| 71 |
key: foo |
| 72 |
increment_float: '20.4' |
| 73 |
""" |
| 74 |
|
| 75 |
RETURN = r""" |
| 76 |
value: |
| 77 |
description: Incremented value of key. |
| 78 |
returned: on success |
| 79 |
type: float |
| 80 |
sample: '4039.4' |
| 81 |
msg: |
| 82 |
description: A short message. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: 'Incremented key: foo by 20.4 to 65.9' |
| 86 |
""" |
| 87 |
|
| 88 |
from ansible.module_utils.basic import AnsibleModule |
| 89 |
|
| 90 |
from ansible_collections.community.general.plugins.module_utils._redis import ( |
| 91 |
RedisAnsible, |
| 92 |
fail_imports, |
| 93 |
redis_auth_argument_spec, |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
def main(): |
| 98 |
redis_auth_args = redis_auth_argument_spec() |
| 99 |
module_args = dict( |
| 100 |
key=dict(type="str", required=True, no_log=False), |
| 101 |
increment_int=dict(type="int"), |
| 102 |
increment_float=dict(type="float"), |
| 103 |
) |
| 104 |
module_args.update(redis_auth_args) |
| 105 |
|
| 106 |
module = AnsibleModule( |
| 107 |
argument_spec=module_args, |
| 108 |
supports_check_mode=True, |
| 109 |
mutually_exclusive=[["increment_int", "increment_float"]], |
| 110 |
) |
| 111 |
fail_imports(module) |
| 112 |
|
| 113 |
redis = RedisAnsible(module) |
| 114 |
key = module.params["key"] |
| 115 |
increment_float = module.params["increment_float"] |
| 116 |
increment_int = module.params["increment_int"] |
| 117 |
increment = 1 |
| 118 |
if increment_float is not None: |
| 119 |
increment = increment_float |
| 120 |
elif increment_int is not None: |
| 121 |
increment = increment_int |
| 122 |
|
| 123 |
result = {"changed": False} |
| 124 |
if module.check_mode: |
| 125 |
value = 0.0 |
| 126 |
try: |
| 127 |
res = redis.connection.get(key) |
| 128 |
if res is not None: |
| 129 |
value = float(res) |
| 130 |
except ValueError: |
| 131 |
msg = f"Value: {res} of key: {key} is not incrementable(int or float)" |
| 132 |
result["msg"] = msg |
| 133 |
module.fail_json(**result) |
| 134 |
except Exception as e: |
| 135 |
msg = f"Failed to get value of key: {key} with exception: {e}" |
| 136 |
result["msg"] = msg |
| 137 |
module.fail_json(**result) |
| 138 |
msg = f"Incremented key: {key} by {increment} to {value + increment}" |
| 139 |
result["msg"] = msg |
| 140 |
result["value"] = float(value + increment) |
| 141 |
module.exit_json(**result) |
| 142 |
|
| 143 |
if increment_float is not None: |
| 144 |
try: |
| 145 |
value = redis.connection.incrbyfloat(key, increment) |
| 146 |
msg = f"Incremented key: {key} by {increment} to {value}" |
| 147 |
result["msg"] = msg |
| 148 |
result["value"] = float(value) |
| 149 |
result["changed"] = True |
| 150 |
module.exit_json(**result) |
| 151 |
except Exception as e: |
| 152 |
msg = f"Failed to increment key: {key} by {increment} with exception: {e}" |
| 153 |
result["msg"] = msg |
| 154 |
module.fail_json(**result) |
| 155 |
elif increment_int is not None: |
| 156 |
try: |
| 157 |
value = redis.connection.incrby(key, increment) |
| 158 |
msg = f"Incremented key: {key} by {increment} to {value}" |
| 159 |
result["msg"] = msg |
| 160 |
result["value"] = float(value) |
| 161 |
result["changed"] = True |
| 162 |
module.exit_json(**result) |
| 163 |
except Exception as e: |
| 164 |
msg = f"Failed to increment key: {key} by {increment} with exception: {e}" |
| 165 |
result["msg"] = msg |
| 166 |
module.fail_json(**result) |
| 167 |
else: |
| 168 |
try: |
| 169 |
value = redis.connection.incr(key) |
| 170 |
msg = f"Incremented key: {key} to {value}" |
| 171 |
result["msg"] = msg |
| 172 |
result["value"] = float(value) |
| 173 |
result["changed"] = True |
| 174 |
module.exit_json(**result) |
| 175 |
except Exception as e: |
| 176 |
msg = f"Failed to increment key: {key} with exception: {e}" |
| 177 |
result["msg"] = msg |
| 178 |
module.fail_json(**result) |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__": |
| 182 |
main() |