fix inventory source parse error handling (#73160)

fixes #51025

added test cases
This commit is contained in:
Brian Coca 2021-01-18 09:33:51 -05:00 committed by GitHub
parent 9d71bbd13e
commit 1e27d4052a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 8 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Correct the inventory source error parse handling, specifically make the config INVENTORY_ANY_UNPARSED_IS_FAILED work as expected.

View file

@ -306,19 +306,24 @@ class InventoryManager(object):
failures.append({'src': source, 'plugin': plugin_name, 'exc': AnsibleError(e), 'tb': tb}) failures.append({'src': source, 'plugin': plugin_name, 'exc': AnsibleError(e), 'tb': tb})
else: else:
display.vvv("%s declined parsing %s as it did not pass its verify_file() method" % (plugin_name, source)) display.vvv("%s declined parsing %s as it did not pass its verify_file() method" % (plugin_name, source))
else:
if not parsed and failures: if not parsed:
# only warn/error if NOT using the default or using it and the file is present
# TODO: handle 'non file' inventorya and detect vs hardcode default
if source != '/etc/ansible/hosts' or os.path.exists(source):
if failures:
# only if no plugin processed files should we show errors. # only if no plugin processed files should we show errors.
for fail in failures: for fail in failures:
display.warning(u'\n* Failed to parse %s with %s plugin: %s' % (to_text(fail['src']), fail['plugin'], to_text(fail['exc']))) display.warning(u'\n* Failed to parse %s with %s plugin: %s' % (to_text(fail['src']), fail['plugin'], to_text(fail['exc'])))
if 'tb' in fail: if 'tb' in fail:
display.vvv(to_text(fail['tb'])) display.vvv(to_text(fail['tb']))
if C.INVENTORY_ANY_UNPARSED_IS_FAILED:
raise AnsibleError(u'Completely failed to parse inventory source %s' % (source)) # final erorr/warning on inventory source failure
if not parsed: if C.INVENTORY_ANY_UNPARSED_IS_FAILED:
if source != '/etc/ansible/hosts' or os.path.exists(source): raise AnsibleError(u'Completely failed to parse inventory source %s' % (source))
# only warn if NOT using the default and if using it, only if the file is present else:
display.warning("Unable to parse %s as an inventory source" % source) display.warning("Unable to parse %s as an inventory source" % source)
# clear up, jic # clear up, jic
self._inventory.current_source = None self._inventory.current_source = None

View file

@ -37,3 +37,15 @@ ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=never ansible-playbook -i ../../inventory
# test extra vars # test extra vars
ansible-inventory -i testhost, -i ./extra_vars_constructed.yml --list -e 'from_extras=hey ' "$@"|grep '"example": "hellohey"' ansible-inventory -i testhost, -i ./extra_vars_constructed.yml --list -e 'from_extras=hey ' "$@"|grep '"example": "hellohey"'
# test parse inventory fail is not an error per config
ANSIBLE_INVENTORY_UNPARSED_FAILED=False ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False ansible -m ping localhost -i /idontexist "$@"
# test no inventory parse is an error with var
[ "$(ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False ansible -m ping localhost -i /idontexist)" != "0" ]
# test single inventory no parse is not an error with var
ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False ansible -m ping localhost -i /idontexist -i ../../invenotory "$@"
# test single inventory no parse is an error with any var
[ "$(ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True ansible -m ping localhost -i /idontexist -i ../../invenotory)" != "0" ]