From eef70d028fc43e8cd9d6c6a6561b4bba21670543 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 10 Apr 2018 17:16:12 -0400 Subject: [PATCH] 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 --- lib/ansible/plugins/inventory/script.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/ansible/plugins/inventory/script.py b/lib/ansible/plugins/inventory/script.py index 253c20bee8..4985a3ef20 100644 --- a/lib/ansible/plugins/inventory/script.py +++ b/lib/ansible/plugins/inventory/script.py @@ -19,6 +19,16 @@ DOCUMENTATION = ''' key: cache env: - 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: - The source provided must an executable that returns Ansible inventory JSON - The source must accept C(--list) and C(--host ) as arguments. @@ -64,7 +74,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): initial_chars = inv_file.read(2) if initial_chars.startswith(b'#!'): shebang_present = True - except: + except Exception: pass if not os.access(path, os.X_OK) and not shebang_present: @@ -99,8 +109,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): if sp.returncode != 0: raise AnsibleError("Inventory script (%s) had an execution error: %s " % (path, err)) - # make sure script output is unicode so that json loader will output - # unicode strings itself + # make sure script output is unicode so that json loader will output unicode strings itself try: data = to_text(stdout, errors="strict") except Exception as e: @@ -111,6 +120,10 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): except Exception as e: 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] 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))