Inventory Script Plugin: raise execution error (#81104)

It adds exception treatment when execute a inventory based on script with the --host argument

---------

Co-authored-by: Everson Leal <everson.leal@sonda.com>
pull/81131/head
Everson Leal 1 year ago committed by GitHub
parent ed8a404f4a
commit 2f820381ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Inventory scripts parser not treat exception when getting hostsvar (https://github.com/ansible/ansible/issues/81103)

@ -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:

@ -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"

Loading…
Cancel
Save