From 230c372aad2b135b1ca6806d9bb264b8a01fcf6f Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Fri, 27 Aug 2021 17:38:20 +0100 Subject: [PATCH] Capture sanity check warnings and display them Captures runtime warnings raise by driver.sanity_check() and display them at the end in case of failure. Related: https://github.com/ansible-community/molecule-podman/pull/76 --- src/molecule/provisioner/ansible_playbook.py | 11 +++++++++-- src/molecule/util.py | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/molecule/provisioner/ansible_playbook.py b/src/molecule/provisioner/ansible_playbook.py index 9d6cd17c06..47d5b25616 100644 --- a/src/molecule/provisioner/ansible_playbook.py +++ b/src/molecule/provisioner/ansible_playbook.py @@ -20,6 +20,7 @@ """Ansible-Playbook Provisioner Module.""" import logging +import warnings from molecule import util @@ -103,12 +104,18 @@ def execute(self): LOG.warning("Skipping, %s action has no playbook." % self._config.action) return - self._config.driver.sanity_checks() - result = util.run_command(self._ansible_command, debug=self._config.debug) + # breakpoint() + with warnings.catch_warnings(record=True) as warns: + # warnings.simplefilter("ignore") + self._config.driver.sanity_checks() + result = util.run_command(self._ansible_command, debug=self._config.debug) + + # print(warns) if result.returncode != 0: util.sysexit_with_message( f"Ansible return code was {result.returncode}, command was: {result.args}", result.returncode, + warns=warns, ) return result.stdout diff --git a/src/molecule/util.py b/src/molecule/util.py index 892c337736..1fcdfff325 100644 --- a/src/molecule/util.py +++ b/src/molecule/util.py @@ -96,7 +96,10 @@ def sysexit(code: int = 1) -> NoReturn: def sysexit_with_message( - msg: str, code: int = 1, detail: Optional[MutableMapping] = None + msg: str, + code: int = 1, + detail: Optional[MutableMapping] = None, + warns: Optional[List[Warning]] = None, ) -> None: """Exit with an error message.""" # detail is usually a multi-line string which is not suitable for normal @@ -108,6 +111,9 @@ def sysexit_with_message( detail_str = str(detail) print(detail_str) LOG.critical(msg) + if warns: + for warn in warns: + LOG.warning(warn.__dict__['message'].args[0]) sysexit(code)