| 1 |
# Copyright (c) 2012, Jan-Piet Mens <jpmens(at)gmail.com> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: redis |
| 9 |
author: |
| 10 |
- Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com> |
| 11 |
- Ansible Core Team |
| 12 |
short_description: Fetch data from Redis |
| 13 |
description: |
| 14 |
- This lookup returns a list of results from a Redis DB corresponding to a list of items given to it. |
| 15 |
requirements: |
| 16 |
- redis (python library https://github.com/andymccurdy/redis-py/) |
| 17 |
options: |
| 18 |
_terms: |
| 19 |
description: List of keys to query. |
| 20 |
type: list |
| 21 |
elements: string |
| 22 |
host: |
| 23 |
description: Location of Redis host. |
| 24 |
type: string |
| 25 |
default: '127.0.0.1' |
| 26 |
env: |
| 27 |
- name: ANSIBLE_REDIS_HOST |
| 28 |
ini: |
| 29 |
- section: lookup_redis |
| 30 |
key: host |
| 31 |
port: |
| 32 |
description: Port on which Redis is listening on. |
| 33 |
default: 6379 |
| 34 |
type: int |
| 35 |
env: |
| 36 |
- name: ANSIBLE_REDIS_PORT |
| 37 |
ini: |
| 38 |
- section: lookup_redis |
| 39 |
key: port |
| 40 |
socket: |
| 41 |
description: Path to socket on which to query Redis, this option overrides host and port options when set. |
| 42 |
type: path |
| 43 |
env: |
| 44 |
- name: ANSIBLE_REDIS_SOCKET |
| 45 |
ini: |
| 46 |
- section: lookup_redis |
| 47 |
key: socket |
| 48 |
""" |
| 49 |
|
| 50 |
EXAMPLES = r""" |
| 51 |
- name: query redis for somekey (default or configured settings used) |
| 52 |
ansible.builtin.debug: |
| 53 |
msg: "{{ lookup('community.general.redis', 'somekey') }}" |
| 54 |
|
| 55 |
- name: query redis for list of keys and non-default host and port |
| 56 |
ansible.builtin.debug: |
| 57 |
msg: "{{ lookup('community.general.redis', item, host='myredis.internal.com', port=2121) }}" |
| 58 |
loop: '{{list_of_redis_keys}}' |
| 59 |
|
| 60 |
- name: use list directly |
| 61 |
ansible.builtin.debug: |
| 62 |
msg: "{{ lookup('community.general.redis', 'key1', 'key2', 'key3') }}" |
| 63 |
|
| 64 |
- name: use list directly with a socket |
| 65 |
ansible.builtin.debug: |
| 66 |
msg: "{{ lookup('community.general.redis', 'key1', 'key2', socket='/var/tmp/redis.sock') }}" |
| 67 |
""" |
| 68 |
|
| 69 |
RETURN = r""" |
| 70 |
_raw: |
| 71 |
description: Value(s) stored in Redis. |
| 72 |
type: list |
| 73 |
elements: str |
| 74 |
""" |
| 75 |
|
| 76 |
HAVE_REDIS = False |
| 77 |
try: |
| 78 |
import redis |
| 79 |
|
| 80 |
HAVE_REDIS = True |
| 81 |
except ImportError: |
| 82 |
pass |
| 83 |
|
| 84 |
from ansible.errors import AnsibleError |
| 85 |
from ansible.module_utils.common.text.converters import to_text |
| 86 |
from ansible.plugins.lookup import LookupBase |
| 87 |
|
| 88 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 89 |
|
| 90 |
|
| 91 |
class LookupModule(LookupBase): |
| 92 |
def run(self, terms, variables, **kwargs): |
| 93 |
if not HAVE_REDIS: |
| 94 |
raise AnsibleError("Can't LOOKUP(redis_kv): module redis is not installed") |
| 95 |
|
| 96 |
# get options |
| 97 |
self.set_options(direct=kwargs) |
| 98 |
check_for_wrong_terms(self, direct=kwargs) |
| 99 |
|
| 100 |
# setup connection |
| 101 |
host = self.get_option("host") |
| 102 |
port = self.get_option("port") |
| 103 |
socket = self.get_option("socket") |
| 104 |
if socket is None: |
| 105 |
conn = redis.Redis(host=host, port=port) |
| 106 |
else: |
| 107 |
conn = redis.Redis(unix_socket_path=socket) |
| 108 |
|
| 109 |
ret = [] |
| 110 |
for term in terms: |
| 111 |
try: |
| 112 |
res = conn.get(term) |
| 113 |
if res is None: |
| 114 |
res = "" |
| 115 |
ret.append(to_text(res)) |
| 116 |
except Exception as e: |
| 117 |
# connection failed or key not found |
| 118 |
raise AnsibleError(f"Encountered exception while fetching {term}: {e}") from e |
| 119 |
return ret |