Ini fixes (#74285)
* avoid 'mixed' param formats * added tests * clog * fixed alignment
This commit is contained in:
parent
8e5dc7306e
commit
c6945de899
3 changed files with 22 additions and 1 deletions
2
changelogs/fragments/ini_lookup_baduser.yml
Normal file
2
changelogs/fragments/ini_lookup_baduser.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- ini lookup - better error on mixed/bad parameters
|
|
@ -65,7 +65,7 @@ import re
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from collections import defaultdict
|
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.six.moves import configparser
|
||||||
from ansible.module_utils._text import to_bytes, to_text, to_native
|
from ansible.module_utils._text import to_bytes, to_text, to_native
|
||||||
from ansible.module_utils.common._collections_compat import MutableSequence
|
from ansible.module_utils.common._collections_compat import MutableSequence
|
||||||
|
@ -132,6 +132,7 @@ class LookupModule(LookupBase):
|
||||||
self._deprecate_inline_kv()
|
self._deprecate_inline_kv()
|
||||||
params = _parse_params(term, paramvals)
|
params = _parse_params(term, paramvals)
|
||||||
try:
|
try:
|
||||||
|
updated_key = False
|
||||||
for param in params:
|
for param in params:
|
||||||
if '=' in param:
|
if '=' in param:
|
||||||
name, value = param.split('=')
|
name, value = param.split('=')
|
||||||
|
@ -141,9 +142,13 @@ class LookupModule(LookupBase):
|
||||||
elif key == term:
|
elif key == term:
|
||||||
# only take first, this format never supported multiple keys inline
|
# only take first, this format never supported multiple keys inline
|
||||||
key = param
|
key = param
|
||||||
|
updated_key = True
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
# bad params passed
|
# bad params passed
|
||||||
raise AnsibleLookupError("Could not use '%s' from '%s': %s" % (param, params, to_native(e)), orig_exc=e)
|
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
|
# TODO: look to use cache to avoid redoing this for every term if they use same file
|
||||||
# Retrieve file path
|
# Retrieve file path
|
||||||
|
|
|
@ -69,3 +69,17 @@
|
||||||
that:
|
that:
|
||||||
- '_.results.0.item == "section1/value1"'
|
- '_.results.0.item == "section1/value1"'
|
||||||
- '_.results.1.item == "section1/value2"'
|
- '_.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'
|
||||||
|
|
Loading…
Reference in a new issue