Lookups: use Ansible's config manager whenever possible (#5440)

* Start using Ansible's config manager to handle options.

* Docs improvements.

* Fix documentation, make options actual lookup options.

* The cyberarkpassword lookup does too strange things.

* The onepassword lookups are converted in #4728, let's not interfere.

* Improve docs.

* Skip shelvefile as well.

* Convert lmdb_kv.

* Convert and fix credstash.

* Convert manifold.

* Drop chef_databag.

* Convert dig.

* Update examples.

* Forgot the most important part.

* Fix lmdb_kv docs.

* Python 2.6 compatibility.

* Convert AnsibleUnicode to str.

* Load lookup with lookup loader.

* Fix environment handling and error message checking.

* Improve docs formatting.
This commit is contained in:
Felix Fontein 2022-11-01 21:58:46 +01:00 committed by GitHub
commit e718bd8445
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 161 additions and 91 deletions

View file

@ -13,15 +13,20 @@ DOCUMENTATION = '''
version_added: '0.2.0'
short_description: fetch data from LMDB
description:
- This lookup returns a list of results from an LMDB DB corresponding to a list of items given to it
- This lookup returns a list of results from an LMDB DB corresponding to a list of items given to it.
requirements:
- lmdb (python library https://lmdb.readthedocs.io/en/release/)
options:
_terms:
description: list of keys to query
description: List of keys to query.
type: list
elements: str
db:
description: path to LMDB database
description: Path to LMDB database.
type: str
default: 'ansible.mdb'
vars:
- name: lmdb_kv_db
'''
EXAMPLES = """
@ -43,8 +48,8 @@ EXAMPLES = """
- item == 'Belgium'
vars:
- lmdb_kv_db: jp.mdb
with_community.general.lmdb_kv:
- be
with_community.general.lmdb_kv:
- be
"""
RETURN = """
@ -58,6 +63,7 @@ _raw:
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible.module_utils.common.text.converters import to_native, to_text
HAVE_LMDB = True
try:
import lmdb
@ -67,8 +73,7 @@ except ImportError:
class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
def run(self, terms, variables=None, **kwargs):
'''
terms contain any number of keys to be retrieved.
If terms is None, all keys from the database are returned
@ -81,17 +86,15 @@ class LookupModule(LookupBase):
vars:
- lmdb_kv_db: "jp.mdb"
'''
if HAVE_LMDB is False:
raise AnsibleError("Can't LOOKUP(lmdb_kv): this module requires lmdb to be installed")
db = variables.get('lmdb_kv_db', None)
if db is None:
db = kwargs.get('db', 'ansible.mdb')
db = str(db)
self.set_options(var_options=variables, direct=kwargs)
db = self.get_option('db')
try:
env = lmdb.open(db, readonly=True)
env = lmdb.open(str(db), readonly=True)
except Exception as e:
raise AnsibleError("LMDB can't open database %s: %s" % (db, to_native(e)))