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))