Skip to content

Commit

Permalink
Avoid pre-run errors when role-name is disabled (#1459)
Browse files Browse the repository at this point in the history
- when `role-name` is added to skip_list, just install simple role name
- when `role-name` is added to warn_list, display warning and continue
  execution.

Fixes: #1454
  • Loading branch information
ssbarnea authored Mar 12, 2021
1 parent aa01b74 commit a6d5e99
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ warn_list:
- skip_this_tag
- git-latest
- experimetal # experimental is included in the implicit list
# - role-name

# Offline mode disables installation of requirements.yml
offline: false
Expand Down
47 changes: 29 additions & 18 deletions src/ansiblelint/_prerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def prepare_environment() -> None:
stderr=subprocess.STDOUT,
)
if run.returncode != 0:
_logger.error(run.stdout, file=sys.stderr)
_logger.error(run.stdout)
sys.exit(run.returncode)

_install_galaxy_role()
Expand All @@ -163,23 +163,34 @@ def _install_galaxy_role() -> None:
if not role_name:
role_name = pathlib.Path(".").absolute().name
role_name = re.sub(r'^{0}'.format(re.escape('ansible-role-')), '', role_name)
fqrn = f"{role_namespace}.{role_name}"
if not re.match(r"[a-z0-9][a-z0-9_]+\.[a-z][a-z0-9_]+$", fqrn):
_logger.error(
"""\
Computed fully qualified role name of %s is not valid.
Please edit meta/main.yml and assure we can correctly determine full role name:
galaxy_info:
role_name: my_name # if absent directory name hosting role is used instead
namespace: my_galaxy_namespace # if absent, author is used instead
Namespace: https://galaxy.ansible.com/docs/contributing/namespaces.html#galaxy-namespace-limitations
Role: https://galaxy.ansible.com/docs/contributing/creating_role.html#role-names
""",
fqrn,
)
sys.exit(INVALID_PREREQUISITES_RC)

if 'role-name' not in options.skip_list:
fqrn = f"{role_namespace}.{role_name}"
if not re.match(r"[a-z0-9][a-z0-9_]+\.[a-z][a-z0-9_]+$", fqrn):
msg = (
"""\
Computed fully qualified role name of %s does not follow current galaxy requirements.
Please edit meta/main.yml and assure we can correctly determine full role name:
galaxy_info:
role_name: my_name # if absent directory name hosting role is used instead
namespace: my_galaxy_namespace # if absent, author is used instead
Namespace: https://galaxy.ansible.com/docs/contributing/namespaces.html#galaxy-namespace-limitations
Role: https://galaxy.ansible.com/docs/contributing/creating_role.html#role-names
As an alternative, you can add 'role-name' to either skip_list or warn_list.
""",
fqrn,
)
if 'role-name' in options.warn_list:
_logger.warning(msg)
else:
_logger.error(msg)
sys.exit(INVALID_PREREQUISITES_RC)
else:
# when 'role-name' is in skip_list, we stick to plain role names
fqrn = role_name
p = pathlib.Path(f"{options.project_dir}/.cache/roles")
p.mkdir(parents=True, exist_ok=True)
link_path = p / f"{role_namespace}.{role_name}"
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def merge_config(file_config: Dict[Any, Any], cli_config: Namespace) -> Namespac
'rulesdir': [],
'skip_list': [],
'tags': [],
'warn_list': ['experimental'],
'warn_list': ['experimental', 'role-name'],
'mock_modules': [],
'mock_roles': [],
'enable_list': [],
Expand Down

0 comments on commit a6d5e99

Please sign in to comment.