Skip to content

Commit

Permalink
src/ansiblelint/prerun.py: Avoid duplicating molecule requirements.ym…
Browse files Browse the repository at this point in the history
…l files

When running ansible-lint on roles with molecule scenarii, it will
run syntax-check on the scenarii but won't find the needed roles
(if any). The current solution is to write a requirements.yml file
in the top of the directory merging all molecule/*/requirements.yml files
but it's duplication and create an extra file only to get ansible-lint
playbook check working.
As a solution, look for molecule/*/requirements.yml and install
dependencies from them too.

Signed-off-by: Arnaud Patard <apatard@hupstream.com>
  • Loading branch information
apatard committed Apr 8, 2021
1 parent 38a5dea commit 9722934
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions src/ansiblelint/prerun.py
Expand Up @@ -101,26 +101,44 @@ def _get_ver_err() -> Tuple[str, str]:
stop=tenacity.stop_after_attempt(3), # type: ignore
before_sleep=tenacity.after_log(_logger, logging.WARNING), # type: ignore
)
def prepare_environment() -> None:
"""Make dependencies available if needed."""
if not options.configured:
# Allow method to be used without calling the command line, so we can
# reuse it in other tools, like molecule.
# pylint: disable=import-outside-toplevel,cyclic-import
from ansiblelint.__main__ import initialize_options
def install_requirements(requirement: str) -> None:
"""Install dependencies from a requirements.yml. """
if not os.path.exists(requirement):
return

initialize_options()
cmd = [
"ansible-galaxy",
"role",
"install",
"--roles-path",
f"{options.project_dir}/.cache/roles",
"-vr",
f"{requirement}",
]

_logger.info("Running %s", " ".join(cmd))
run = subprocess.run(
cmd,
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
_logger.error(run.stdout)
raise RuntimeError(run.returncode)

if not options.offline and os.path.exists("requirements.yml"):
# Run galaxy collection install works on v2 requirements.yml
if "collections" in yaml_from_file(requirement):

cmd = [
"ansible-galaxy",
"role",
"collection",
"install",
"--roles-path",
f"{options.project_dir}/.cache/roles",
"-p",
f"{options.project_dir}/.cache/collections",
"-vr",
"requirements.yml",
f"{requirement}",
]

_logger.info("Running %s", " ".join(cmd))
Expand All @@ -133,32 +151,23 @@ def prepare_environment() -> None:
)
if run.returncode != 0:
_logger.error(run.stdout)
sys.exit(run.returncode)

# Run galaxy collection install works on v2 requirements.yml
if "collections" in yaml_from_file("requirements.yml"):

cmd = [
"ansible-galaxy",
"collection",
"install",
"-p",
f"{options.project_dir}/.cache/collections",
"-vr",
"requirements.yml",
]

_logger.info("Running %s", " ".join(cmd))
run = subprocess.run(
cmd,
universal_newlines=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
_logger.error(run.stdout)
raise RuntimeError(run.returncode)
raise RuntimeError(run.returncode)


def prepare_environment() -> None:
"""Make dependencies available if needed."""
if not options.configured:
# Allow method to be used without calling the command line, so we can
# reuse it in other tools, like molecule.
# pylint: disable=import-outside-toplevel,cyclic-import
from ansiblelint.__main__ import initialize_options

initialize_options()

if not options.offline:
install_requirements("requirements.yml")
for req in pathlib.Path(".").glob("molecule/*/requirements.yml"):
install_requirements(str(req))

_install_galaxy_role()
_perform_mockings()
Expand Down

0 comments on commit 9722934

Please sign in to comment.