Skip to content

Commit

Permalink
Handle exceptions in interpreter discovery (ansible#81745)
Browse files Browse the repository at this point in the history
* Handle exceptions like AnsibleError, AnsibleConnectionFailure
  raise while interpreter discovery.

Fixes: ansible#78264

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde authored Sep 21, 2023
1 parent 62c1019 commit 86fd702
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/interpreter_discovery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- interpreter_discovery - handle AnsibleError exception raised while interpreter discovery (https://github.com/ansible/ansible/issues/78264).
3 changes: 3 additions & 0 deletions lib/ansible/executor/interpreter_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
Expand Down Expand Up @@ -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)))
Expand Down
12 changes: 12 additions & 0 deletions test/units/executor/test_interpreter_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)

0 comments on commit 86fd702

Please sign in to comment.