restrore showing stderr on script success (#38177)

* restore showing stderr on script success

accidentally removed during transition to plugin, with toggle for those
who prefer the quiet way

fixes #33776

* stderr display if no other errors capture first
* fixed issue with error encoding

(cherry picked from commit eef70d028f)
This commit is contained in:
Brian Coca 2018-04-10 17:16:12 -04:00 committed by Sam Doran
parent 0f524a1632
commit 78484137c0

View file

@ -19,6 +19,16 @@ DOCUMENTATION = '''
key: cache key: cache
env: env:
- name: ANSIBLE_INVENTORY_PLUGIN_SCRIPT_CACHE - name: ANSIBLE_INVENTORY_PLUGIN_SCRIPT_CACHE
always_show_stderr:
description: Toggle display of stderr even when script was successful
version_added: "2.5.1"
default: True
type: boolean
ini:
- section: inventory_plugin_script
key: always_show_stderr
env:
- name: ANSIBLE_INVENTORY_PLUGIN_SCRIPT_STDERR
description: description:
- The source provided must an executable that returns Ansible inventory JSON - The source provided must an executable that returns Ansible inventory JSON
- The source must accept C(--list) and C(--host <hostname>) as arguments. - The source must accept C(--list) and C(--host <hostname>) as arguments.
@ -64,7 +74,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
initial_chars = inv_file.read(2) initial_chars = inv_file.read(2)
if initial_chars.startswith(b'#!'): if initial_chars.startswith(b'#!'):
shebang_present = True shebang_present = True
except: except Exception:
pass pass
if not os.access(path, os.X_OK) and not shebang_present: if not os.access(path, os.X_OK) and not shebang_present:
@ -99,8 +109,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
if sp.returncode != 0: if sp.returncode != 0:
raise AnsibleError("Inventory script (%s) had an execution error: %s " % (path, err)) raise AnsibleError("Inventory script (%s) had an execution error: %s " % (path, err))
# make sure script output is unicode so that json loader will output # make sure script output is unicode so that json loader will output unicode strings itself
# unicode strings itself
try: try:
data = to_text(stdout, errors="strict") data = to_text(stdout, errors="strict")
except Exception as e: except Exception as e:
@ -111,6 +120,10 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
except Exception as e: except Exception as e:
raise AnsibleError("failed to parse executable inventory script results from {0}: {1}\n{2}".format(path, to_native(e), err)) raise AnsibleError("failed to parse executable inventory script results from {0}: {1}\n{2}".format(path, to_native(e), err))
# if no other errors happened and you want to force displaying stderr, do so now
if err and self.get_option('always_show_stderr'):
self.display.error(msg=to_text(err))
processed = self._cache[cache_key] processed = self._cache[cache_key]
if not isinstance(processed, Mapping): if not isinstance(processed, Mapping):
raise AnsibleError("failed to parse executable inventory script results from {0}: needs to be a json dict\n{1}".format(path, err)) raise AnsibleError("failed to parse executable inventory script results from {0}: needs to be a json dict\n{1}".format(path, err))