diff --git a/changelogs/fragments/ini_lookup_baduser.yml b/changelogs/fragments/ini_lookup_baduser.yml new file mode 100644 index 00000000000..64580c9007c --- /dev/null +++ b/changelogs/fragments/ini_lookup_baduser.yml @@ -0,0 +1,2 @@ +bugfixes: + - ini lookup - better error on mixed/bad parameters diff --git a/lib/ansible/plugins/lookup/ini.py b/lib/ansible/plugins/lookup/ini.py index b88d4e5adfa..b8ba792ef11 100644 --- a/lib/ansible/plugins/lookup/ini.py +++ b/lib/ansible/plugins/lookup/ini.py @@ -65,7 +65,7 @@ import re from io import StringIO from collections import defaultdict -from ansible.errors import AnsibleLookupError +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.common._collections_compat import MutableSequence @@ -132,6 +132,7 @@ class LookupModule(LookupBase): self._deprecate_inline_kv() params = _parse_params(term, paramvals) try: + updated_key = False for param in params: if '=' in param: name, value = param.split('=') @@ -141,9 +142,13 @@ class LookupModule(LookupBase): elif key == term: # only take first, this format never supported multiple keys inline key = param + updated_key = True except ValueError as e: # bad params passed raise AnsibleLookupError("Could not use '%s' from '%s': %s" % (param, params, to_native(e)), orig_exc=e) + if not updated_key: + raise AnsibleOptionsError("No key to lookup was provided as first term with in string inline options: %s" % term) + # only passed options in inline string # TODO: look to use cache to avoid redoing this for every term if they use same file # Retrieve file path diff --git a/test/integration/targets/lookup_ini/test_lookup_properties.yml b/test/integration/targets/lookup_ini/test_lookup_properties.yml index 0d39334113d..915c68eeb6e 100644 --- a/test/integration/targets/lookup_ini/test_lookup_properties.yml +++ b/test/integration/targets/lookup_ini/test_lookup_properties.yml @@ -69,3 +69,17 @@ that: - '_.results.0.item == "section1/value1"' - '_.results.1.item == "section1/value2"' + + + - name: capture bad behaviour + block: + - name: mix options type and push key out of order + debug: msg="{{ lookup('ini', 'file=lookup.ini', 'value1', section='value_section') }}" + register: bad_mojo + ignore_errors: true + + - name: verify + assert: + that: + - bad_mojo is failed + - '"No key to lookup was provided as first term with in string inline option" in bad_mojo.msg'