Skip to content

Commit

Permalink
Ini fixes (#74285)
Browse files Browse the repository at this point in the history
* avoid 'mixed' param formats

* added tests

* clog

* fixed alignment
  • Loading branch information
bcoca committed Apr 15, 2021
1 parent 8e5dc73 commit c6945de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/ini_lookup_baduser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ini lookup - better error on mixed/bad parameters
7 changes: 6 additions & 1 deletion lib/ansible/plugins/lookup/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
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
Expand Down Expand Up @@ -132,6 +132,7 @@ def run(self, terms, variables=None, **kwargs):
self._deprecate_inline_kv()
params = _parse_params(term, paramvals)
try:
updated_key = False
for param in params:
if '=' in param:
name, value = param.split('=')
Expand All @@ -141,9 +142,13 @@ def run(self, terms, variables=None, **kwargs):
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
Expand Down
14 changes: 14 additions & 0 deletions test/integration/targets/lookup_ini/test_lookup_properties.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit c6945de

Please sign in to comment.