diff --git a/testinfra/backend/ansible.py b/testinfra/backend/ansible.py index 61bf25f8..534c5bd1 100644 --- a/testinfra/backend/ansible.py +++ b/testinfra/backend/ansible.py @@ -55,7 +55,15 @@ def run(self, command: str, *args: str, **kwargs: Any) -> base.CommandResult: ) if host is not None: return host.run(command) - out = self.run_ansible("shell", module_args=command, check=False) + + ansible_connection = self.ansible_runner.get_variables(self.host).get( + "ansible_connection") + + module_name = "shell" + if ansible_connection == "winrm": + module_name = "win_shell" + + out = self.run_ansible(module_name, module_args=command, check=False) return self.result( out["rc"], self.encode(command), diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index 2aefab94..afb66d93 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -279,15 +279,19 @@ def parse_containerspec(containerspec: str) -> tuple[str, Optional[str]]: def get_encoding(self) -> str: encoding = None - for python in ("python3", "python"): - cmd = self.run( - "%s -c 'import locale;print(locale.getpreferredencoding())'", - python, - encoding=None, - ) - if cmd.rc == 0: - encoding = cmd.stdout_bytes.splitlines()[0].decode("ascii") - break + try: + for python in ("python3", "python"): + + cmd = self.run( + "%s -c 'import locale;print(locale.getpreferredencoding())'", + python, + encoding=None, + ) + if cmd.rc == 0: + encoding = cmd.stdout_bytes.splitlines()[0].decode("ascii") + break + except Exception as e: + logger.warning("Problem occurred during detecting encoding: ", e) # Python is not installed, we hope the encoding to be the same as # local machine... if not encoding: