From fe03a1f9c451b577e5b0c731eb4de09adb732ca1 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 10:58:58 +0200 Subject: [PATCH 01/20] Start using Ansible's config manager to handle options. --- changelogs/fragments/lookup-options.yml | 17 +++++++++++++++++ plugins/lookup/cartesian.py | 1 + plugins/lookup/chef_databag.py | 2 ++ plugins/lookup/credstash.py | 5 +++-- plugins/lookup/cyberarkpassword.py | 1 + plugins/lookup/dependent.py | 2 ++ plugins/lookup/dig.py | 3 ++- plugins/lookup/dnstxt.py | 1 + plugins/lookup/filetree.py | 2 ++ plugins/lookup/flattened.py | 5 +++-- plugins/lookup/hiera.py | 4 +++- plugins/lookup/keyring.py | 4 +++- plugins/lookup/lmdb_kv.py | 6 +++--- plugins/lookup/manifold.py | 8 +++++++- plugins/lookup/onepassword.py | 2 ++ plugins/lookup/onepassword_raw.py | 2 ++ plugins/lookup/shelvefile.py | 1 + 17 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/lookup-options.yml diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml new file mode 100644 index 00000000000..bb010facb8e --- /dev/null +++ b/changelogs/fragments/lookup-options.yml @@ -0,0 +1,17 @@ +minor_changes: + - "cartesian lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "chef_databag lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "credstash lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "cyberarkpassword lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "dependent lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "dig lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "dnstxt lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "filetree lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "flattened lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "hiera lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "keyring lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "lmdb_kv lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "manifold lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "onepassword lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "onepassword_raw lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "shelvefile lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/cartesian.py b/plugins/lookup/cartesian.py index 516d153389d..f73cdc3ba08 100644 --- a/plugins/lookup/cartesian.py +++ b/plugins/lookup/cartesian.py @@ -69,6 +69,7 @@ def _lookup_variables(self, terms): return results def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) terms = self._lookup_variables(terms) diff --git a/plugins/lookup/chef_databag.py b/plugins/lookup/chef_databag.py index b14d924ae87..14d71d4e811 100644 --- a/plugins/lookup/chef_databag.py +++ b/plugins/lookup/chef_databag.py @@ -91,6 +91,8 @@ def run(self, terms, variables=None, **kwargs): if not HAS_CHEF: raise AnsibleError('PyChef needed for lookup plugin, try `pip install pychef`') + self.set_options(var_options=variables, direct=kwargs) + for term in terms: self.parse_kv_args(parse_kv(term)) diff --git a/plugins/lookup/credstash.py b/plugins/lookup/credstash.py index a783f8ba083..6d8c9e3c915 100644 --- a/plugins/lookup/credstash.py +++ b/plugins/lookup/credstash.py @@ -100,11 +100,12 @@ class LookupModule(LookupBase): - def run(self, terms, variables, **kwargs): - + def run(self, terms, variables=None, **kwargs): if not CREDSTASH_INSTALLED: raise AnsibleError('The credstash lookup plugin requires credstash to be installed.') + self.set_options(var_options=variables, direct=kwargs) + ret = [] for term in terms: try: diff --git a/plugins/lookup/cyberarkpassword.py b/plugins/lookup/cyberarkpassword.py index 00dd81d7f7e..d3dd39a732c 100644 --- a/plugins/lookup/cyberarkpassword.py +++ b/plugins/lookup/cyberarkpassword.py @@ -174,6 +174,7 @@ class LookupModule(LookupBase): """ def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) display.vvvv("%s" % terms) if isinstance(terms, list): diff --git a/plugins/lookup/dependent.py b/plugins/lookup/dependent.py index 0bd234c1b4e..99e5b8677cc 100644 --- a/plugins/lookup/dependent.py +++ b/plugins/lookup/dependent.py @@ -180,6 +180,8 @@ def __process(self, result, terms, index, current, templar, variables): def run(self, terms, variables=None, **kwargs): """Generate list.""" + self.set_options(var_options=variables, direct=kwargs) + result = [] if len(terms) > 0: templar = Templar(loader=self._templar._loader) diff --git a/plugins/lookup/dig.py b/plugins/lookup/dig.py index 3d91152aeae..87c84fd3146 100644 --- a/plugins/lookup/dig.py +++ b/plugins/lookup/dig.py @@ -279,10 +279,11 @@ def run(self, terms, variables=None, **kwargs): ... flat=0 # returns a dict; default is 1 == string ''' - if HAVE_DNS is False: raise AnsibleError("The dig lookup requires the python 'dnspython' library and it is not installed") + self.set_options(var_options=variables, direct=kwargs) + # Create Resolver object so that we can set NS if necessary myres = dns.resolver.Resolver(configure=True) edns_size = 4096 diff --git a/plugins/lookup/dnstxt.py b/plugins/lookup/dnstxt.py index abf3e64b606..396b93f4366 100644 --- a/plugins/lookup/dnstxt.py +++ b/plugins/lookup/dnstxt.py @@ -71,6 +71,7 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) if HAVE_DNS is False: raise AnsibleError("Can't LOOKUP(dnstxt): module dns.resolver is not installed") diff --git a/plugins/lookup/filetree.py b/plugins/lookup/filetree.py index 13b3d71e438..f12cc45192f 100644 --- a/plugins/lookup/filetree.py +++ b/plugins/lookup/filetree.py @@ -201,6 +201,8 @@ def file_props(root, path): class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + basedir = self.get_basedir(variables) ret = [] diff --git a/plugins/lookup/flattened.py b/plugins/lookup/flattened.py index 25098fa23a7..365220232d9 100644 --- a/plugins/lookup/flattened.py +++ b/plugins/lookup/flattened.py @@ -78,9 +78,10 @@ def _do_flatten(self, terms, variables): return ret - def run(self, terms, variables, **kwargs): - + def run(self, terms, variables=None, **kwargs): if not isinstance(terms, list): raise AnsibleError("with_flattened expects a list") + self.set_options(var_options=variables, direct=kwargs) + return self._do_flatten(terms, variables) diff --git a/plugins/lookup/hiera.py b/plugins/lookup/hiera.py index 055d16133ba..296bbebf318 100644 --- a/plugins/lookup/hiera.py +++ b/plugins/lookup/hiera.py @@ -85,7 +85,9 @@ def get(self, hiera_key): class LookupModule(LookupBase): - def run(self, terms, variables=''): + def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + hiera = Hiera() ret = [hiera.get(terms)] return ret diff --git a/plugins/lookup/keyring.py b/plugins/lookup/keyring.py index 56718c32e90..c1fbb450603 100644 --- a/plugins/lookup/keyring.py +++ b/plugins/lookup/keyring.py @@ -53,10 +53,12 @@ class LookupModule(LookupBase): - def run(self, terms, **kwargs): + def run(self, terms, variables=None, **kwargs): if not HAS_KEYRING: raise AnsibleError(u"Can't LOOKUP(keyring): missing required python library 'keyring'") + self.set_options(var_options=variables, direct=kwargs) + display.vvvv(u"keyring: %s" % keyring.get_keyring()) ret = [] for term in terms: diff --git a/plugins/lookup/lmdb_kv.py b/plugins/lookup/lmdb_kv.py index 569a49a5f06..520dc596e8d 100644 --- a/plugins/lookup/lmdb_kv.py +++ b/plugins/lookup/lmdb_kv.py @@ -67,8 +67,7 @@ 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,10 +80,11 @@ def run(self, terms, variables, **kwargs): 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") + self.set_options(var_options=variables, direct=kwargs) + db = variables.get('lmdb_kv_db', None) if db is None: db = kwargs.get('db', 'ansible.mdb') diff --git a/plugins/lookup/manifold.py b/plugins/lookup/manifold.py index 9d2913063ab..7af6660aba1 100644 --- a/plugins/lookup/manifold.py +++ b/plugins/lookup/manifold.py @@ -207,7 +207,7 @@ def get_credentials(self, resource_id): class LookupModule(LookupBase): - def run(self, terms, variables=None, api_token=None, project=None, team=None): + def run(self, terms, variables=None, **kwargs): """ :param terms: a list of resources lookups to run. :param variables: ansible variables active at the time of the lookup @@ -217,6 +217,12 @@ def run(self, terms, variables=None, api_token=None, project=None, team=None): :return: a dictionary of resources credentials """ + self.set_options(var_options=variables, direct=kwargs) + + api_token = kwargs.get('api_token') + project = kwargs.get('project') + team = kwargs.get('team') + if not api_token: api_token = os.getenv('MANIFOLD_API_TOKEN') if not api_token: diff --git a/plugins/lookup/onepassword.py b/plugins/lookup/onepassword.py index 42e07e9cbfd..1d15891f9a7 100644 --- a/plugins/lookup/onepassword.py +++ b/plugins/lookup/onepassword.py @@ -268,6 +268,8 @@ def _parse_field(self, data_json, field_name, section_title=None): class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + op = OnePass() field = kwargs.get('field', 'password') diff --git a/plugins/lookup/onepassword_raw.py b/plugins/lookup/onepassword_raw.py index 9a1e0741a06..fdb3eccd291 100644 --- a/plugins/lookup/onepassword_raw.py +++ b/plugins/lookup/onepassword_raw.py @@ -76,6 +76,8 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + op = OnePass() vault = kwargs.get('vault') diff --git a/plugins/lookup/shelvefile.py b/plugins/lookup/shelvefile.py index c1aaef83cef..0692bd45c52 100644 --- a/plugins/lookup/shelvefile.py +++ b/plugins/lookup/shelvefile.py @@ -53,6 +53,7 @@ def read_shelve(self, shelve_filename, key): return res def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) if not isinstance(terms, list): terms = [terms] From 0ea0e1b752dd08dfb9c341d8cf2115419aa00ec9 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:06:13 +0200 Subject: [PATCH 02/20] Docs improvements. --- plugins/lookup/cartesian.py | 4 +++- plugins/lookup/dependent.py | 2 +- plugins/lookup/flattened.py | 8 +++++--- plugins/lookup/keyring.py | 4 +++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/plugins/lookup/cartesian.py b/plugins/lookup/cartesian.py index f73cdc3ba08..d76e8f532ac 100644 --- a/plugins/lookup/cartesian.py +++ b/plugins/lookup/cartesian.py @@ -15,9 +15,11 @@ - It is clearer with an example, it turns [1, 2, 3], [a, b] into [1, a], [1, b], [2, a], [2, b], [3, a], [3, b]. You can see the exact syntax in the examples section. options: - _raw: + _terms: description: - a set of lists + type: list + elements: list required: true ''' diff --git a/plugins/lookup/dependent.py b/plugins/lookup/dependent.py index 99e5b8677cc..b44a9208af5 100644 --- a/plugins/lookup/dependent.py +++ b/plugins/lookup/dependent.py @@ -16,7 +16,7 @@ or template expressions which evaluate to lists or dicts, composed of the elements of the input evaluated lists and dictionaries." options: - _raw: + _terms: description: - A list where the elements are one-element dictionaries, mapping a name to a string, list, or dictionary. The name is the index that is used in the result object. The value is iterated over as described below. diff --git a/plugins/lookup/flattened.py b/plugins/lookup/flattened.py index 365220232d9..0f5818d0ae6 100644 --- a/plugins/lookup/flattened.py +++ b/plugins/lookup/flattened.py @@ -11,14 +11,16 @@ author: Serge van Ginderachter (!UNKNOWN) short_description: return single list completely flattened description: - - given one or more lists, this lookup will flatten any list elements found recursively until only 1 list is left. + - Given one or more lists, this lookup will flatten any list elements found recursively until only 1 list is left. options: _terms: description: lists to flatten + type: list + elements: raw required: true notes: - - unlike 'items' which only flattens 1 level, this plugin will continue to flatten until it cannot find lists anymore. - - aka highlander plugin, there can only be one (list). + - Unlike 'items' which only flattens 1 level, this plugin will continue to flatten until it cannot find lists anymore. + - Aka highlander plugin, there can only be one (list). ''' EXAMPLES = """ diff --git a/plugins/lookup/keyring.py b/plugins/lookup/keyring.py index c1fbb450603..a4c914ed1a6 100644 --- a/plugins/lookup/keyring.py +++ b/plugins/lookup/keyring.py @@ -26,7 +26,9 @@ - 'servicename username' - name: access mysql with password from keyring - mysql_db: login_password={{lookup('community.general.keyring','mysql joe')}} login_user=joe + community.mysql.mysql_db: + login_password: "{{ lookup('community.general.keyring', 'mysql joe') }}" + login_user: joe """ RETURN = """ From 5d2c355f1c38ef4be246cb7acbd46a22f0eb40d1 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:12:43 +0200 Subject: [PATCH 03/20] Fix documentation, make options actual lookup options. --- changelogs/fragments/lookup-options.yml | 2 +- plugins/lookup/hiera.py | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index bb010facb8e..f018a5b491d 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -8,7 +8,7 @@ minor_changes: - "dnstxt lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "filetree lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "flattened lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "hiera lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "hiera lookup plugin - start using Ansible's configuration manager to parse options. The Hiera executable and config file can now also be passed as lookup parameters (https://github.com/ansible-collections/community.general/pull/5440)." - "keyring lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "lmdb_kv lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "manifold lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/hiera.py b/plugins/lookup/hiera.py index 296bbebf318..c74780d9313 100644 --- a/plugins/lookup/hiera.py +++ b/plugins/lookup/hiera.py @@ -14,23 +14,23 @@ requirements: - hiera (command line utility) description: - - Retrieves data from an Puppetmaster node using Hiera as ENC + - Retrieves data from an Puppetmaster node using Hiera as ENC. options: - _hiera_key: + _terms: description: - - The list of keys to lookup on the Puppetmaster + - The list of keys to lookup on the Puppetmaster. type: list elements: string required: true - _bin_file: + executable: description: - - Binary file to execute Hiera + - Binary file to execute Hiera. default: '/usr/bin/hiera' env: - name: ANSIBLE_HIERA_BIN - _hierarchy_file: + config_file: description: - - File that describes the hierarchy of Hiera + - File that describes the hierarchy of Hiera. default: '/etc/hiera.yaml' env: - name: ANSIBLE_HIERA_CFG @@ -67,11 +67,12 @@ from ansible.utils.cmd_functions import run_cmd from ansible.module_utils.common.text.converters import to_text -ANSIBLE_HIERA_CFG = os.getenv('ANSIBLE_HIERA_CFG', '/etc/hiera.yaml') -ANSIBLE_HIERA_BIN = os.getenv('ANSIBLE_HIERA_BIN', '/usr/bin/hiera') - class Hiera(object): + def __init__(self, hiera_cfg, hiera_bin): + self.hiera_cfg = hiera_cfg + self.hiera_bin = hiera_bin + def get(self, hiera_key): pargs = [ANSIBLE_HIERA_BIN] pargs.extend(['-c', ANSIBLE_HIERA_CFG]) @@ -88,6 +89,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): self.set_options(var_options=variables, direct=kwargs) - hiera = Hiera() + hiera = Hiera(self.get_option('config_file'), self.get_option('executable')) ret = [hiera.get(terms)] return ret From 1fdf8d016a87d3492043e27a990fc912dd7bbbb4 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:14:58 +0200 Subject: [PATCH 04/20] The cyberarkpassword lookup does too strange things. --- changelogs/fragments/lookup-options.yml | 1 - plugins/lookup/cyberarkpassword.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index f018a5b491d..5661cf73c01 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -2,7 +2,6 @@ minor_changes: - "cartesian lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "chef_databag lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "credstash lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "cyberarkpassword lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "dependent lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "dig lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "dnstxt lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/cyberarkpassword.py b/plugins/lookup/cyberarkpassword.py index d3dd39a732c..1e005e23e83 100644 --- a/plugins/lookup/cyberarkpassword.py +++ b/plugins/lookup/cyberarkpassword.py @@ -174,8 +174,6 @@ class LookupModule(LookupBase): """ def run(self, terms, variables=None, **kwargs): - self.set_options(var_options=variables, direct=kwargs) - display.vvvv("%s" % terms) if isinstance(terms, list): return_values = [] From 771794f2c2c194fb84fd51c07d49f28731defe41 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:16:49 +0200 Subject: [PATCH 05/20] The onepassword lookups are converted in #4728, let's not interfere. --- changelogs/fragments/lookup-options.yml | 2 -- plugins/lookup/onepassword.py | 2 -- plugins/lookup/onepassword_raw.py | 2 -- 3 files changed, 6 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index 5661cf73c01..07ee665ef1c 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -11,6 +11,4 @@ minor_changes: - "keyring lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "lmdb_kv lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "manifold lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "onepassword lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "onepassword_raw lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "shelvefile lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/onepassword.py b/plugins/lookup/onepassword.py index 1d15891f9a7..42e07e9cbfd 100644 --- a/plugins/lookup/onepassword.py +++ b/plugins/lookup/onepassword.py @@ -268,8 +268,6 @@ def _parse_field(self, data_json, field_name, section_title=None): class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - self.set_options(var_options=variables, direct=kwargs) - op = OnePass() field = kwargs.get('field', 'password') diff --git a/plugins/lookup/onepassword_raw.py b/plugins/lookup/onepassword_raw.py index fdb3eccd291..9a1e0741a06 100644 --- a/plugins/lookup/onepassword_raw.py +++ b/plugins/lookup/onepassword_raw.py @@ -76,8 +76,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - self.set_options(var_options=variables, direct=kwargs) - op = OnePass() vault = kwargs.get('vault') From 6b88154cf32aaf4261361e051a97a08df7dd5fe2 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:18:15 +0200 Subject: [PATCH 06/20] Improve docs. --- plugins/lookup/shelvefile.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/lookup/shelvefile.py b/plugins/lookup/shelvefile.py index 0692bd45c52..4573236b4ce 100644 --- a/plugins/lookup/shelvefile.py +++ b/plugins/lookup/shelvefile.py @@ -14,23 +14,24 @@ - Read keys from Python shelve file. options: _terms: - description: sets of key value pairs of parameters + description: Sets of key value pairs of parameters. key: - description: key to query + description: Key to query. required: true file: - description: path to shelve file + description: Path to shelve file. required: true ''' EXAMPLES = """ -- name: retrieve a string value corresponding to a key inside a Python shelve file - ansible.builtin.debug: msg="{{ lookup('community.general.shelvefile', 'file=path_to_some_shelve_file.db key=key_to_retrieve') }} +- name: Retrieve a string value corresponding to a key inside a Python shelve file + ansible.builtin.debug: + msg: "{{ lookup('community.general.shelvefile', 'file=path_to_some_shelve_file.db key=key_to_retrieve') }}" """ RETURN = """ _list: - description: value(s) of key(s) in shelve file(s) + description: Value(s) of key(s) in shelve file(s). type: list elements: str """ From da16c30d10723e2d5de25a07e81c7aefae8d461e Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:18:38 +0200 Subject: [PATCH 07/20] Skip shelvefile as well. --- changelogs/fragments/lookup-options.yml | 1 - plugins/lookup/shelvefile.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index 07ee665ef1c..3c8db66e7ff 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -11,4 +11,3 @@ minor_changes: - "keyring lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "lmdb_kv lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "manifold lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "shelvefile lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/shelvefile.py b/plugins/lookup/shelvefile.py index 4573236b4ce..35f1097c8b6 100644 --- a/plugins/lookup/shelvefile.py +++ b/plugins/lookup/shelvefile.py @@ -54,8 +54,6 @@ def read_shelve(self, shelve_filename, key): return res def run(self, terms, variables=None, **kwargs): - self.set_options(var_options=variables, direct=kwargs) - if not isinstance(terms, list): terms = [terms] From a197bcd7c062fc89faf328359859aa98848d40fe Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:22:06 +0200 Subject: [PATCH 08/20] Convert lmdb_kv. --- plugins/lookup/lmdb_kv.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/plugins/lookup/lmdb_kv.py b/plugins/lookup/lmdb_kv.py index 520dc596e8d..030cee0698d 100644 --- a/plugins/lookup/lmdb_kv.py +++ b/plugins/lookup/lmdb_kv.py @@ -13,15 +13,20 @@ 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 + type: str default: 'ansible.mdb' + vars: + - lmdb_kv_db ''' EXAMPLES = """ @@ -43,8 +48,8 @@ - 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 @@ 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 @@ -85,10 +91,7 @@ def run(self, terms, variables=None, **kwargs): self.set_options(var_options=variables, direct=kwargs) - db = variables.get('lmdb_kv_db', None) - if db is None: - db = kwargs.get('db', 'ansible.mdb') - db = str(db) + db = self.get_option('db') try: env = lmdb.open(db, readonly=True) From a5f61ccca5cdebbe8800fed15630b61e27ac649f Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:27:33 +0200 Subject: [PATCH 09/20] Convert and fix credstash. --- changelogs/fragments/lookup-options.yml | 2 ++ plugins/lookup/credstash.py | 40 ++++++++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index 3c8db66e7ff..404263e16da 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -11,3 +11,5 @@ minor_changes: - "keyring lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "lmdb_kv lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "manifold lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." +bugfixes: + - "credstash lookup plugin - pass plugin options to credstash for all terms, not just for the first (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/credstash.py b/plugins/lookup/credstash.py index 6d8c9e3c915..d256d6d24ee 100644 --- a/plugins/lookup/credstash.py +++ b/plugins/lookup/credstash.py @@ -22,25 +22,33 @@ required: true table: description: name of the credstash table to query + type: str default: 'credential-store' version: description: Credstash version + type: str + default: '' region: description: AWS region + type: str profile_name: description: AWS profile to use for authentication + type: str env: - name: AWS_PROFILE aws_access_key_id: description: AWS access key ID + type: str env: - name: AWS_ACCESS_KEY_ID aws_secret_access_key: description: AWS access key + type: str env: - name: AWS_SECRET_ACCESS_KEY aws_session_token: description: AWS session token + type: str env: - name: AWS_SESSION_TOKEN ''' @@ -106,23 +114,33 @@ def run(self, terms, variables=None, **kwargs): self.set_options(var_options=variables, direct=kwargs) + version = self.get_option('version') + region = self.get_option('region') + table = self.get_option('table') + profile_name = self.get_option('profile_name') + aws_access_key_id = self.get_option('aws_access_key_id') + aws_secret_access_key = self.get_option('aws_secret_access_key') + aws_session_token = self.get_option('aws_session_token') + + context = { + k: v for k, v in kwargs.items() + if k not in ('version', 'region', 'table', 'profile_name', 'aws_access_key_id', 'aws_secret_access_key', 'aws_session_token') + } + + kwargs_pass = { + 'profile_name': profile_name, + 'aws_access_key_id': aws_access_key_id, + 'aws_secret_access_key': aws_secret_access_key, + 'aws_session_token': aws_session_token, + } + ret = [] for term in terms: try: - version = kwargs.pop('version', '') - region = kwargs.pop('region', None) - table = kwargs.pop('table', 'credential-store') - profile_name = kwargs.pop('profile_name', os.getenv('AWS_PROFILE', None)) - aws_access_key_id = kwargs.pop('aws_access_key_id', os.getenv('AWS_ACCESS_KEY_ID', None)) - aws_secret_access_key = kwargs.pop('aws_secret_access_key', os.getenv('AWS_SECRET_ACCESS_KEY', None)) - aws_session_token = kwargs.pop('aws_session_token', os.getenv('AWS_SESSION_TOKEN', None)) - kwargs_pass = {'profile_name': profile_name, 'aws_access_key_id': aws_access_key_id, - 'aws_secret_access_key': aws_secret_access_key, 'aws_session_token': aws_session_token} - val = credstash.getSecret(term, version, region, table, context=kwargs, **kwargs_pass) + ret.append(credstash.getSecret(term, version, region, table, context=context, **kwargs_pass)) except credstash.ItemNotFound: raise AnsibleError('Key {0} not found'.format(term)) except Exception as e: raise AnsibleError('Encountered exception while fetching {0}: {1}'.format(term, e)) - ret.append(val) return ret From 93779b1a76ae20f21aa116605f2d55a97744ca30 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:28:44 +0200 Subject: [PATCH 10/20] Convert manifold. --- plugins/lookup/manifold.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/plugins/lookup/manifold.py b/plugins/lookup/manifold.py index 7af6660aba1..51064b9c2b2 100644 --- a/plugins/lookup/manifold.py +++ b/plugins/lookup/manifold.py @@ -219,14 +219,9 @@ def run(self, terms, variables=None, **kwargs): self.set_options(var_options=variables, direct=kwargs) - api_token = kwargs.get('api_token') - project = kwargs.get('project') - team = kwargs.get('team') - - if not api_token: - api_token = os.getenv('MANIFOLD_API_TOKEN') - if not api_token: - raise AnsibleError('API token is required. Please set api_token parameter or MANIFOLD_API_TOKEN env var') + api_token = self.get_option('api_token') + project = self.get_option('project') + team = self.get_option('team') try: labels = terms From a9be200adfa76056a8feec98d9f9e50131c877ea Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:30:25 +0200 Subject: [PATCH 11/20] Drop chef_databag. --- changelogs/fragments/lookup-options.yml | 1 - plugins/lookup/chef_databag.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index 404263e16da..6afe00e5fde 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -1,6 +1,5 @@ minor_changes: - "cartesian lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "chef_databag lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "credstash lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "dependent lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "dig lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/chef_databag.py b/plugins/lookup/chef_databag.py index 14d71d4e811..b14d924ae87 100644 --- a/plugins/lookup/chef_databag.py +++ b/plugins/lookup/chef_databag.py @@ -91,8 +91,6 @@ def run(self, terms, variables=None, **kwargs): if not HAS_CHEF: raise AnsibleError('PyChef needed for lookup plugin, try `pip install pychef`') - self.set_options(var_options=variables, direct=kwargs) - for term in terms: self.parse_kv_args(parse_kv(term)) From 982e30f5c345fb678bfacb7c16f649f00ae68f60 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:39:55 +0200 Subject: [PATCH 12/20] Convert dig. --- changelogs/fragments/lookup-options.yml | 2 +- plugins/lookup/dig.py | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/changelogs/fragments/lookup-options.yml b/changelogs/fragments/lookup-options.yml index 6afe00e5fde..1fb406a3830 100644 --- a/changelogs/fragments/lookup-options.yml +++ b/changelogs/fragments/lookup-options.yml @@ -2,7 +2,7 @@ minor_changes: - "cartesian lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "credstash lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "dependent lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - - "dig lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." + - "dig lookup plugin - start using Ansible's configuration manager to parse options. All documented options can now also be passed as lookup parameters (https://github.com/ansible-collections/community.general/pull/5440)." - "dnstxt lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "filetree lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." - "flattened lookup plugin - start using Ansible's configuration manager to parse options (https://github.com/ansible-collections/community.general/pull/5440)." diff --git a/plugins/lookup/dig.py b/plugins/lookup/dig.py index 87c84fd3146..e30a54cb7fb 100644 --- a/plugins/lookup/dig.py +++ b/plugins/lookup/dig.py @@ -29,14 +29,18 @@ options: _terms: description: Domain(s) to query. + type: list + elements: str qtype: description: - Record type to query. - C(DLV) has been removed in community.general 6.0.0. + type: str default: 'A' choices: [A, ALL, AAAA, CNAME, DNAME, DNSKEY, DS, HINFO, LOC, MX, NAPTR, NS, NSEC3PARAM, PTR, RP, RRSIG, SOA, SPF, SRV, SSHFP, TLSA, TXT] flat: description: If 0 each record is returned as a dictionary, otherwise a string. + type: int default: 1 retry_servfail: description: Retry a nameserver if it returns SERVFAIL. @@ -59,6 +63,11 @@ default: false type: bool version_added: 6.0.0 + class: + description: + - "Class." + type: str + default: 'IN' notes: - ALL is not a record per-se, merely the listed fields are available for any record results you retrieve in the form of a dictionary. - While the 'dig' lookup plugin supports anything which dnspython supports out of the box, only a subset can be converted into a dictionary. @@ -290,11 +299,15 @@ def run(self, terms, variables=None, **kwargs): myres.use_edns(0, ednsflags=dns.flags.DO, payload=edns_size) domain = None - qtype = 'A' - flat = True - fail_on_error = False - real_empty = False - rdclass = dns.rdataclass.from_text('IN') + qtype = self.get_option('qtype') + flat = self.get_option('flat') + fail_on_error = self.get_option('fail_on_error') + real_empty = self.get_option('real_empty') + try: + rdclass = dns.rdataclass.from_text(self.get_option('class')) + except Exception as e: + raise AnsibleError("dns lookup illegal CLASS: %s" % to_native(e)) + myres.retry_servfail = self.get_option('retry_servfail') for t in terms: if t.startswith('@'): # e.g. "@10.0.1.2,192.0.2.1" is ok. @@ -317,7 +330,7 @@ def run(self, terms, variables=None, **kwargs): continue if '=' in t: try: - opt, arg = t.split('=') + opt, arg = t.split('=', 1) except Exception: pass From 74e65db089169bb555647671ce35d40d93d606c7 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 11:42:25 +0200 Subject: [PATCH 13/20] Update examples. --- plugins/lookup/dig.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/lookup/dig.py b/plugins/lookup/dig.py index e30a54cb7fb..ceaff15e9f7 100644 --- a/plugins/lookup/dig.py +++ b/plugins/lookup/dig.py @@ -21,8 +21,8 @@ - In addition to (default) A record, it is also possible to specify a different record type that should be queried. This can be done by either passing-in additional parameter of format qtype=TYPE to the dig lookup, or by appending /TYPE to the FQDN being queried. - If multiple values are associated with the requested record, the results will be returned as a comma-separated list. - In such cases you may want to pass option wantlist=True to the plugin, which will result in the record values being returned as a list - over which you can iterate later on. + In such cases you may want to pass option I(wantlist=true) to the lookup call, or alternatively use C(query) instead of C(lookup), + which will result in the record values being returned as a list over which you can iterate later on. - By default, the lookup will rely on system-wide configured DNS servers for performing the query. It is also possible to explicitly specify DNS servers to query using the @DNS_SERVER_1,DNS_SERVER_2,...,DNS_SERVER_N notation. This needs to be passed-in as an additional parameter to the lookup @@ -83,7 +83,7 @@ - name: "The TXT record for example.org." ansible.builtin.debug: - msg: "{{ lookup('community.general.dig', 'example.org.', 'qtype=TXT') }}" + msg: "{{ lookup('community.general.dig', 'example.org.', qtype='TXT') }}" - name: "The TXT record for example.org, alternative syntax." ansible.builtin.debug: @@ -92,24 +92,24 @@ - name: use in a loop ansible.builtin.debug: msg: "MX record for gmail.com {{ item }}" - with_items: "{{ lookup('community.general.dig', 'gmail.com./MX', wantlist=True) }}" + with_items: "{{ lookup('community.general.dig', 'gmail.com./MX', wantlist=true) }}" - ansible.builtin.debug: msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '192.0.2.5/PTR') }}" - ansible.builtin.debug: msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa./PTR') }}" - ansible.builtin.debug: - msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa.', 'qtype=PTR') }}" + msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa.', qtype='PTR') }}" - ansible.builtin.debug: msg: "Querying 198.51.100.23 for IPv4 address for example.com. produces {{ lookup('dig', 'example.com', '@198.51.100.23') }}" - ansible.builtin.debug: msg: "XMPP service for gmail.com. is available at {{ item.target }} on port {{ item.port }}" - with_items: "{{ lookup('community.general.dig', '_xmpp-server._tcp.gmail.com./SRV', 'flat=0', wantlist=True) }}" + with_items: "{{ lookup('community.general.dig', '_xmpp-server._tcp.gmail.com./SRV', flat=0, wantlist=true) }}" - name: Retry nameservers that return SERVFAIL ansible.builtin.debug: - msg: "{{ lookup('community.general.dig', 'example.org./A', 'retry_servfail=True') }}" + msg: "{{ lookup('community.general.dig', 'example.org./A', retry_servfail=true) }}" """ RETURN = """ From da89ad0f86e8dbadf836e42b98e620dd52402143 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 12:06:45 +0200 Subject: [PATCH 14/20] Forgot the most important part. --- plugins/lookup/hiera.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/lookup/hiera.py b/plugins/lookup/hiera.py index c74780d9313..1049e80b027 100644 --- a/plugins/lookup/hiera.py +++ b/plugins/lookup/hiera.py @@ -74,13 +74,13 @@ def __init__(self, hiera_cfg, hiera_bin): self.hiera_bin = hiera_bin def get(self, hiera_key): - pargs = [ANSIBLE_HIERA_BIN] - pargs.extend(['-c', ANSIBLE_HIERA_CFG]) + pargs = [self.hiera_bin] + pargs.extend(['-c', self.hiera_cfg]) pargs.extend(hiera_key) rc, output, err = run_cmd("{0} -c {1} {2}".format( - ANSIBLE_HIERA_BIN, ANSIBLE_HIERA_CFG, hiera_key[0])) + self.hiera_bin, self.hiera_cfg, hiera_key[0])) return to_text(output.strip()) From 607a90aa102f53b5376018736958fd888aab432a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 12:08:40 +0200 Subject: [PATCH 15/20] Fix lmdb_kv docs. --- plugins/lookup/lmdb_kv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/lookup/lmdb_kv.py b/plugins/lookup/lmdb_kv.py index 030cee0698d..88cb257d626 100644 --- a/plugins/lookup/lmdb_kv.py +++ b/plugins/lookup/lmdb_kv.py @@ -22,11 +22,11 @@ type: list elements: str db: - description: path to LMDB database + description: Path to LMDB database. type: str default: 'ansible.mdb' vars: - - lmdb_kv_db + - name: lmdb_kv_db ''' EXAMPLES = """ From 5e44c7cc9a98798d315c54e4fa3888e51f13dc89 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 12:25:38 +0200 Subject: [PATCH 16/20] Python 2.6 compatibility. --- plugins/lookup/credstash.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/lookup/credstash.py b/plugins/lookup/credstash.py index d256d6d24ee..d49d5b23cb5 100644 --- a/plugins/lookup/credstash.py +++ b/plugins/lookup/credstash.py @@ -122,10 +122,10 @@ def run(self, terms, variables=None, **kwargs): aws_secret_access_key = self.get_option('aws_secret_access_key') aws_session_token = self.get_option('aws_session_token') - context = { - k: v for k, v in kwargs.items() + context = dict( + (k, v) for k, v in kwargs.items() if k not in ('version', 'region', 'table', 'profile_name', 'aws_access_key_id', 'aws_secret_access_key', 'aws_session_token') - } + ) kwargs_pass = { 'profile_name': profile_name, From d0e781c96003e7df7537bdaaf326cfd9b347ccac Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 12:30:49 +0200 Subject: [PATCH 17/20] Convert AnsibleUnicode to str. --- plugins/lookup/lmdb_kv.py | 2 +- tests/integration/targets/lookup_lmdb_kv/test.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/lookup/lmdb_kv.py b/plugins/lookup/lmdb_kv.py index 88cb257d626..0950249dc85 100644 --- a/plugins/lookup/lmdb_kv.py +++ b/plugins/lookup/lmdb_kv.py @@ -94,7 +94,7 @@ def run(self, terms, variables=None, **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))) diff --git a/tests/integration/targets/lookup_lmdb_kv/test.yml b/tests/integration/targets/lookup_lmdb_kv/test.yml index 3c39d4e251f..217c020cac7 100644 --- a/tests/integration/targets/lookup_lmdb_kv/test.yml +++ b/tests/integration/targets/lookup_lmdb_kv/test.yml @@ -6,10 +6,10 @@ - hosts: localhost tasks: - debug: - msg: '{{ query(''community.general.lmdb_kv'', ''nl'', ''be'', ''lu'', db=''jp.mdb'') }}' + msg: '{{ query("community.general.lmdb_kv", "nl", "be", "lu", db="jp.mdb") }}' - debug: var: item.1 - loop: '{{ query(''community.general.lmdb_kv'', db=''jp.mdb'') }}' + loop: '{{ query("community.general.lmdb_kv", db="jp.mdb") }}' - assert: that: - query('community.general.lmdb_kv', 'nl', 'be', 'lu', db='jp.mdb') == ['Netherlands', 'Belgium', 'Luxembourg'] From e73ef78f915c7bcd3c6b5ce2de95c402c0c09e04 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 12:33:32 +0200 Subject: [PATCH 18/20] Load lookup with lookup loader. --- tests/unit/plugins/lookup/test_manifold.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/plugins/lookup/test_manifold.py b/tests/unit/plugins/lookup/test_manifold.py index e9fd912b2b7..daf7197ea5d 100644 --- a/tests/unit/plugins/lookup/test_manifold.py +++ b/tests/unit/plugins/lookup/test_manifold.py @@ -11,6 +11,7 @@ from ansible.module_utils.urls import ConnectionError, SSLValidationError from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError from ansible.module_utils import six +from ansible.plugins.loader import lookup_loader from ansible_collections.community.general.plugins.lookup.manifold import ManifoldApiClient, LookupModule, ApiError import json @@ -375,8 +376,7 @@ def test_get_credentials(self, open_url_mock): class TestLookupModule(unittest.TestCase): def setUp(self): - self.lookup = LookupModule() - self.lookup._load_name = "manifold" + self.lookup = lookup_loader.get('community.general.manifold') @patch('ansible_collections.community.general.plugins.lookup.manifold.ManifoldApiClient') def test_get_all(self, client_mock): From def21f419f3c58a9d5aca626b87c3b2eba26e025 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 29 Oct 2022 12:46:48 +0200 Subject: [PATCH 19/20] Fix environment handling and error message checking. --- tests/unit/plugins/lookup/test_manifold.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/unit/plugins/lookup/test_manifold.py b/tests/unit/plugins/lookup/test_manifold.py index daf7197ea5d..f6f4a605a66 100644 --- a/tests/unit/plugins/lookup/test_manifold.py +++ b/tests/unit/plugins/lookup/test_manifold.py @@ -14,6 +14,7 @@ from ansible.plugins.loader import lookup_loader from ansible_collections.community.general.plugins.lookup.manifold import ManifoldApiClient, LookupModule, ApiError import json +import os API_FIXTURES = { @@ -515,23 +516,22 @@ def test_catch_unhandled_exception(self, client_mock): self.lookup.run([], api_token='token-123') self.assertTrue('Exception: Unknown error' in str(context.exception)) - @patch('ansible_collections.community.general.plugins.lookup.manifold.os.getenv') @patch('ansible_collections.community.general.plugins.lookup.manifold.ManifoldApiClient') - def test_falls_back_to_env_var(self, client_mock, getenv_mock): - getenv_mock.return_value = 'token-321' + def test_falls_back_to_env_var(self, client_mock): client_mock.return_value.get_resources.return_value = [] client_mock.return_value.get_credentials.return_value = [] - self.lookup.run([]) - getenv_mock.assert_called_with('MANIFOLD_API_TOKEN') + try: + os.environ['MANIFOLD_API_TOKEN'] = 'token-321' + self.lookup.run([]) + finally: + os.environ.pop('MANIFOLD_API_TOKEN', None) client_mock.assert_called_with('token-321') - @patch('ansible_collections.community.general.plugins.lookup.manifold.os.getenv') @patch('ansible_collections.community.general.plugins.lookup.manifold.ManifoldApiClient') - def test_falls_raises_on_no_token(self, client_mock, getenv_mock): - getenv_mock.return_value = None + def test_falls_raises_on_no_token(self, client_mock): client_mock.return_value.get_resources.return_value = [] client_mock.return_value.get_credentials.return_value = [] + os.environ.pop('MANIFOLD_API_TOKEN', None) with self.assertRaises(AnsibleError) as context: self.lookup.run([]) - self.assertEqual('API token is required. Please set api_token parameter or MANIFOLD_API_TOKEN env var', - str(context.exception)) + assert 'api_token' in str(context.exception) From 93351f7c47fd8c1914a6115ba4db9be610e544c6 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 1 Nov 2022 21:45:01 +0100 Subject: [PATCH 20/20] Improve docs formatting. --- .github/workflows/docs-pr.yml | 1 + plugins/lookup/flattened.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docs-pr.yml b/.github/workflows/docs-pr.yml index 26213d705d3..04a50dc2014 100644 --- a/.github/workflows/docs-pr.yml +++ b/.github/workflows/docs-pr.yml @@ -26,6 +26,7 @@ jobs: init-fail-on-error: true provide-link-targets: | ansible_collections.ansible.builtin.dict2items_filter + ansible_collections.ansible.builtin.items_lookup ansible_collections.ansible.builtin.path_join_filter ansible_collections.community.kubevirt.kubevirt_cdi_upload_module ansible_collections.community.kubevirt.kubevirt_inventory diff --git a/plugins/lookup/flattened.py b/plugins/lookup/flattened.py index 0f5818d0ae6..0f290e559d4 100644 --- a/plugins/lookup/flattened.py +++ b/plugins/lookup/flattened.py @@ -19,7 +19,8 @@ elements: raw required: true notes: - - Unlike 'items' which only flattens 1 level, this plugin will continue to flatten until it cannot find lists anymore. + - Unlike the R(items lookup,ansible_collections.ansible.builtin.items_lookup) which only flattens 1 level, + this plugin will continue to flatten until it cannot find lists anymore. - Aka highlander plugin, there can only be one (list). '''