diff options
author | Abhijeet Kasurde <akasurde@redhat.com> | 2023-09-21 20:13:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-21 20:13:40 +0200 |
commit | 86fd7026a88988c224ae175a281e7e6e2f3c5bc3 (patch) | |
tree | 03b056f8ea931e31d0dbe16b2a9a61e5d158f081 | |
parent | Add intentional tests for dnf list (#81738) (diff) | |
download | ansible-86fd7026a88988c224ae175a281e7e6e2f3c5bc3.tar.xz ansible-86fd7026a88988c224ae175a281e7e6e2f3c5bc3.zip |
Handle exceptions in interpreter discovery (#81745)
* Handle exceptions like AnsibleError, AnsibleConnectionFailure
raise while interpreter discovery.
Fixes: #78264
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r-- | changelogs/fragments/interpreter_discovery.yml | 3 | ||||
-rw-r--r-- | lib/ansible/executor/interpreter_discovery.py | 3 | ||||
-rw-r--r-- | test/units/executor/test_interpreter_discovery.py | 12 |
3 files changed, 18 insertions, 0 deletions
diff --git a/changelogs/fragments/interpreter_discovery.yml b/changelogs/fragments/interpreter_discovery.yml new file mode 100644 index 0000000000..0e8a7c1093 --- /dev/null +++ b/changelogs/fragments/interpreter_discovery.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- interpreter_discovery - handle AnsibleError exception raised while interpreter discovery (https://github.com/ansible/ansible/issues/78264). diff --git a/lib/ansible/executor/interpreter_discovery.py b/lib/ansible/executor/interpreter_discovery.py index c95cf2ed8f..283e790c1c 100644 --- a/lib/ansible/executor/interpreter_discovery.py +++ b/lib/ansible/executor/interpreter_discovery.py @@ -10,6 +10,7 @@ import pkgutil import re from ansible import constants as C +from ansible.errors import AnsibleError from ansible.module_utils.common.text.converters import to_native, to_text from ansible.module_utils.distro import LinuxDistribution from ansible.utils.display import Display @@ -150,6 +151,8 @@ def discover_interpreter(action, interpreter_name, discovery_mode, task_vars): return platform_interpreter except NotImplementedError as ex: display.vvv(msg=u'Python interpreter discovery fallback ({0})'.format(to_text(ex)), host=host) + except AnsibleError: + raise except Exception as ex: if not is_silent: display.warning(msg=u'Unhandled error in Python interpreter discovery for host {0}: {1}'.format(host, to_text(ex))) diff --git a/test/units/executor/test_interpreter_discovery.py b/test/units/executor/test_interpreter_discovery.py index 10fc64be75..e30e98d036 100644 --- a/test/units/executor/test_interpreter_discovery.py +++ b/test/units/executor/test_interpreter_discovery.py @@ -6,10 +6,12 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import pytest from unittest.mock import MagicMock from ansible.executor.interpreter_discovery import discover_interpreter from ansible.module_utils.common.text.converters import to_text +from ansible.errors import AnsibleConnectionFailure mock_ubuntu_platform_res = to_text( r'{"osrelease_content": "NAME=\"Ubuntu\"\nVERSION=\"16.04.5 LTS (Xenial Xerus)\"\nID=ubuntu\nID_LIKE=debian\n' @@ -84,3 +86,13 @@ def test_no_interpreters_found(): assert mock_action.method_calls[1][0] == '_discovery_warnings.append' assert u'No python interpreters found for host host-fóöbär (tried' \ in mock_action.method_calls[1][1][0] + + +def test_ansible_error_exception(): + mock_action = MagicMock() + mock_action._low_level_execute_command.side_effect = AnsibleConnectionFailure("host key mismatch") + + with pytest.raises(AnsibleConnectionFailure) as context: + discover_interpreter(mock_action, 'python', 'auto_legacy', {'inventory_hostname': u'host'}) + + assert 'host key mismatch' == str(context.value) |