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

Avoid overriding role paths defaults #1361

Merged
merged 1 commit into from Feb 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/ansiblelint/_prerun.py
Expand Up @@ -8,6 +8,7 @@

from ansiblelint.config import ansible_collections_path, collection_list, options
from ansiblelint.constants import (
ANSIBLE_DEFAULT_ROLES_PATH,
ANSIBLE_MIN_VERSION,
ANSIBLE_MISSING_RC,
ANSIBLE_MOCKED_MODULE,
Expand Down Expand Up @@ -95,11 +96,6 @@ def _prepare_ansible_paths() -> None:
library_paths: List[str] = []
roles_path: List[str] = []

if 'ANSIBLE_ROLES_PATH' in os.environ:
roles_path = os.environ['ANSIBLE_ROLES_PATH'].split(':')
if 'ANSIBLE_LIBRARY' in os.environ:
library_paths = os.environ['ANSIBLE_LIBRARY'].split(':')

for path_list, path in (
(library_paths, "plugins/modules"),
(library_paths, ".cache/modules"),
Expand All @@ -112,7 +108,7 @@ def _prepare_ansible_paths() -> None:

_update_env('ANSIBLE_LIBRARY', library_paths)
_update_env(ansible_collections_path(), collection_list)
_update_env('ANSIBLE_ROLES_PATH', roles_path)
_update_env('ANSIBLE_ROLES_PATH', roles_path, default=ANSIBLE_DEFAULT_ROLES_PATH)


def _make_module_stub(module_name: str) -> None:
Expand Down Expand Up @@ -154,9 +150,10 @@ def _write_module_stub(
f.write(body)


def _update_env(varname: str, value: List[str]) -> None:
"""Update environment variable if needed."""
def _update_env(varname: str, value: List[str], default: str = "") -> None:
"""Update colon based environment variable if needed. by appending."""
if value:
value = [*os.environ.get(varname, default=default).split(':'), *value]
value_str = ":".join(value)
if value_str != os.environ.get(varname, ""):
os.environ[varname] = value_str
Expand Down
5 changes: 5 additions & 0 deletions src/ansiblelint/constants.py
Expand Up @@ -19,6 +19,11 @@
# Minimal version of Ansible we support for runtime
ANSIBLE_MIN_VERSION = "2.9"

# Based on https://docs.ansible.com/ansible/latest/reference_appendices/config.html
ANSIBLE_DEFAULT_ROLES_PATH = (
"~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles"
)

ANSIBLE_MOCKED_MODULE = """\
# This is a mocked Ansible module generated by ansible-lint
from ansible.module_utils.basic import AnsibleModule
Expand Down
10 changes: 8 additions & 2 deletions test/TestCliRolePaths.py
Expand Up @@ -107,7 +107,10 @@ def test_run_role_name_with_prefix(self):

result = run_ansible_lint(role_path, cwd=cwd)
assert len(result.stdout) == 0
assert "Added ANSIBLE_ROLES_PATH=roles:.cache/roles" in result.stderr
assert (
"Added ANSIBLE_ROLES_PATH=~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles:.cache/roles"
in result.stderr
)
assert result.returncode == 0

def test_run_role_name_from_meta(self):
Expand All @@ -116,7 +119,10 @@ def test_run_role_name_from_meta(self):

result = run_ansible_lint(role_path, cwd=cwd)
assert len(result.stdout) == 0
assert "Added ANSIBLE_ROLES_PATH=roles:.cache/roles" in result.stderr
assert (
"Added ANSIBLE_ROLES_PATH=~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles:.cache/roles"
in result.stderr
)
assert result.returncode == 0

def test_run_invalid_role_name_from_meta(self):
Expand Down