diff --git a/changelogs/fragments/81104-inventory-script-plugin-raise-execution-error.yml b/changelogs/fragments/81104-inventory-script-plugin-raise-execution-error.yml new file mode 100644 index 00000000000..924d314fae8 --- /dev/null +++ b/changelogs/fragments/81104-inventory-script-plugin-raise-execution-error.yml @@ -0,0 +1,2 @@ +bugfixes: +- Inventory scripts parser not treat exception when getting hostsvar (https://github.com/ansible/ansible/issues/81103) diff --git a/lib/ansible/plugins/inventory/script.py b/lib/ansible/plugins/inventory/script.py index 0d03476f544..3cdd99b6e59 100644 --- a/lib/ansible/plugins/inventory/script.py +++ b/lib/ansible/plugins/inventory/script.py @@ -187,7 +187,11 @@ class InventoryModule(BaseInventoryPlugin): sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError as e: raise AnsibleError("problem running %s (%s)" % (' '.join(cmd), e)) - (out, err) = sp.communicate() + (out, stderr) = sp.communicate() + + if sp.returncode != 0: + raise AnsibleError("Inventory script (%s) had an execution error: %s" % (path, to_native(stderr))) + if out.strip() == '': return {} try: diff --git a/test/units/plugins/inventory/test_script.py b/test/units/plugins/inventory/test_script.py index f4a172a4ddb..89eb4f5b5d0 100644 --- a/test/units/plugins/inventory/test_script.py +++ b/test/units/plugins/inventory/test_script.py @@ -103,3 +103,11 @@ class TestInventoryModule(unittest.TestCase): self.inventory_module.parse(self.inventory, self.loader, '/foo/bar/foobar.py') assert e.value.message == to_native("failed to parse executable inventory script results from " "/foo/bar/foobar.py: needs to be a json dict\ndummyédata\n") + + def test_get_host_variables_subprocess_script_raises_error(self): + self.popen_result.returncode = 1 + self.popen_result.stderr = to_bytes("dummyéerror") + + with pytest.raises(AnsibleError) as e: + self.inventory_module.get_host_variables('/foo/bar/foobar.py', 'dummy host') + assert e.value.message == "Inventory script (/foo/bar/foobar.py) had an execution error: dummyéerror"