ansible / ansible-lint

ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
https://ansible.readthedocs.io/projects/lint/
GNU General Public License v3.0
3.47k stars 654 forks source link

ansible-lint should allow role names beginning with a underscore #4370

Open gardar opened 4 days ago

gardar commented 4 days ago
Summary

Recently we added a ansible role for common shared tasks to the prometheus ansible collection. The role is named _common (fqcn prometheus.prometheus._common) to distinguish it from the other roles in the collection, as it is a internal role that's only supposed to be used by the other roles and not directly by a user.

ansible-lint generates false positives for that particular role, the role-name rule and the var-naming[no-role-prefix] rule. The error message for the var-naming[no-role-prefix] rule suggests that the variable should be named exactly like it is already named, for example:

var-naming[no-role-prefix]: Variables names from within roles should use _common_ as a prefix. (vars: _common_service_name)
var-naming[no-role-prefix]: Variables names from within roles should use _common_ as a prefix. (vars: __common_binary_basename)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_local_cache_path)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binaries)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binary_name)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binary_install_dir)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_config_dir)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binary_url)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_checksums_url)

I know the Ansible docs suggest that role names should not begin with a underscore, but Ansible does not seem to have any issues with consuming a role with a name like this, and neither does ansible galaxy have any issues with importing the role. I've tested with every ansible version between 2.9 - 2.17.

Issue Type
OS / ENVIRONMENT
Run ansible/ansible-lint@main
  with:
    setup_python: true
    expected_return_code: 0
  env:
    ANSIBLE_FORCE_COLOR: true
    ANSIBLE_GALAXY_SERVER_GALAXY_URL: https://galaxy.ansible.com/
    ANSIBLE_GALAXY_SERVER_GALAXY_TIMEOUT: 120
    ANSIBLE_GALAXY_SERVER_LIST: galaxy

Using ansible-lint github action.

STEPS TO REPRODUCE
Desired Behavior

ansible-lint should allow ansible role names beginning with _. At least when they are a part of a collection, continue to disallow a _ prefix in standalone roles is probably a good idea.

Actual Behavior
role-name: Role name _common does not match ``^[a-z][a-z0-9_]*$`` pattern.
roles/_common:1
Error: Role name _common does not match ``^\[a-z]\[a-z0-9_]*$`` pattern.
var-naming[no-role-prefix]: Variables names from within roles should use _common_ as a prefix. (vars: _common_service_name)
var-naming[no-role-prefix]: Variables names from within roles should use _common_ as a prefix. (vars: __common_binary_basename)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_local_cache_path)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binaries)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binary_name)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binary_install_dir)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_config_dir)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_binary_url)
Error: Variables names from within roles should use _common_ as a prefix. (vars: _common_checksums_url)

Please give some details of what is happening. Include a [minimum complete verifiable example] with:

cavcrosby commented 3 days ago

I wonder if this implies that there is a bug elsewhere (e.g. docs, Galaxy), as according to the documentation, the verbiage suggests roles within a collection starting with an underscore shouldn't be able to be imported via Galaxy.

The collection import into Galaxy will fail if a role name does not comply with these rules.

gardar commented 8 hours ago

Definitely looks like incorrect information in the docs. Perhaps galaxy struggled with this in the past or maybe it's an issue with standalone roles. Or maybe someone just thought it would be ugly to name roles like that 😃. In any case, I haven't seen any scenario where that name causes the role to fail.

I've opened up another issue over at https://github.com/ansible/ansible-documentation/issues/2035 and a forum discussion as well. Hopefully someone can shed some light on this.