diff options
author | Sam Doran <sdoran@redhat.com> | 2021-05-12 16:57:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-12 16:57:02 -0400 |
commit | 0affe4d027ef4ca5517c06da44dcd1b5b8e2544c (patch) | |
tree | d633da759f92d4734aad2fa7ce1b7cdf95fde45b /lib | |
parent | 829c9c3d46b98fe1135e3c5a457714807b9e5544 (diff) | |
download | ansible-0affe4d027ef4ca5517c06da44dcd1b5b8e2544c.tar.gz |
ini lookup - catch and handle duplicate key and missing section errors (#74629)
* Remove unused import
* Add integration tests for errors
* Cleanup formatting on existing tests
* Add changelog
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/plugins/lookup/ini.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/ansible/plugins/lookup/ini.py b/lib/ansible/plugins/lookup/ini.py index b8ba792ef1..eff6413dea 100644 --- a/lib/ansible/plugins/lookup/ini.py +++ b/lib/ansible/plugins/lookup/ini.py @@ -15,7 +15,7 @@ DOCUMENTATION = """ - "You can also read a property file which - in this case - does not contain section." options: _terms: - description: The key(s) to look up + description: The key(s) to look up. required: True type: description: Type of the file. 'properties' refers to the Java properties files. @@ -67,7 +67,7 @@ from collections import defaultdict from ansible.errors import AnsibleLookupError, AnsibleOptionsError from ansible.module_utils.six.moves import configparser -from ansible.module_utils._text import to_bytes, to_text, to_native +from ansible.module_utils._text import to_text, to_native from ansible.module_utils.common._collections_compat import MutableSequence from ansible.plugins.lookup import LookupBase @@ -167,8 +167,15 @@ class LookupModule(LookupBase): config.write(contents) config.seek(0, os.SEEK_SET) - self.cp.readfp(config) - var = self.get_value(key, paramvals['section'], paramvals['default'], paramvals['re']) + try: + self.cp.readfp(config) + except configparser.DuplicateOptionError as doe: + raise AnsibleLookupError("Duplicate option in '{file}': {error}".format(file=paramvals['file'], error=to_native(doe))) + + try: + var = self.get_value(key, paramvals['section'], paramvals['default'], paramvals['re']) + except configparser.NoSectionError: + raise AnsibleLookupError("No section '{section}' in {file}".format(section=paramvals['section'], file=paramvals['file'])) if var is not None: if isinstance(var, MutableSequence): for v in var: |