Skip to content

Commit

Permalink
Improve output when Ansible is missing (#2956)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 8, 2020
1 parent f4344b5 commit 697aceb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
24 changes: 8 additions & 16 deletions lib/molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"""Config Module."""

import os
import subprocess
from uuid import uuid4

from packaging.version import Version
Expand All @@ -30,7 +29,7 @@
from molecule.dependency import ansible_galaxy, shell
from molecule.model import schema_v3
from molecule.provisioner import ansible
from molecule.util import boolean, lru_cache, sysexit
from molecule.util import boolean, lru_cache, run_command, sysexit

LOG = logger.get_logger(__name__)
MOLECULE_DEBUG = boolean(os.environ.get("MOLECULE_DEBUG", "False"))
Expand Down Expand Up @@ -446,28 +445,21 @@ def set_env_from_file(env, env_file):


@lru_cache()
def ansible_version(version: str = None) -> Version:
def ansible_version(version: str = "") -> Version:
"""Return current Version object for Ansible.
If version is not mentioned, it returns current version as detected.
When version argument is mentioned, it return converts the version string
to Version object in order to make it usable in comparisons.
"""
if not version:
try:
version = (
subprocess.run(
["ansible", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
.stdout.splitlines()[0]
.split()[1]
)
except FileNotFoundError:
proc = run_command(["ansible", "--version"], quiet=True)
if proc.returncode == 0:
version = proc.stdout.splitlines()[0].split()[1]
else:
LOG.fatal(
"Unable to find ansible executable. Read https://molecule.readthedocs.io/en/latest/installation.html"
"Unable to find a working copy of ansible executable. Read https://molecule.readthedocs.io/en/latest/installation.html\n%s",
proc,
)
sysexit(RC_SETUP_ERROR)
return Version(version)
8 changes: 6 additions & 2 deletions lib/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def sysexit_with_message(
sysexit(code)


def run_command(cmd, env=None, debug=False, echo=False) -> CompletedProcess:
def run_command(
cmd, env=None, debug=False, echo=False, quiet=False
) -> CompletedProcess:
"""
Execute the given command and returns None.
Expand Down Expand Up @@ -131,7 +133,9 @@ def run_command(cmd, env=None, debug=False, echo=False) -> CompletedProcess:
if debug:
print_environment_vars(env)

return run(args, env=env, stdout=stdout, stderr=stderr, echo=echo or debug)
return run(
args, env=env, stdout=stdout, stderr=stderr, echo=echo or debug, quiet=quiet
)


def os_walk(directory, pattern, excludes=[]):
Expand Down

0 comments on commit 697aceb

Please sign in to comment.