restrore showing stderr on script success (#38177)

* restrore 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
pull/38242/head
Brian Coca 7 years ago committed by GitHub
parent 1850bb752f
commit eef70d028f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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.6"
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))

Loading…
Cancel
Save