Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve output when Ansible is missing #2956

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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