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.46k stars 654 forks source link

rules var-naming[no-role-prefix] and var-naming[pattern] collide if var_naming_pattern is changed from default #4142

Open mhallin2 opened 4 months ago

mhallin2 commented 4 months ago
Summary

Role variable prefix does not work if var_naming_pattern: has been altered from its default.

Bradley Thornton has requested me to create this as a bug.

If the var_naming_pattern setting in .ansible-lint.cfg file is changed from its default and you create a role and use set_fact to set a variable The rules var-naming[no-role-prefix] and var-naming[pattern] will collide as both scenarios can not be fulfilled

Issue Type
OS / ENVIRONMENT
ansible-lint --version
ansible-lint 24.2.3 using ansible-core:2.16.6 ansible-compat:4.1.11 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8
STEPS TO REPRODUCE

update .ansible-lint with the below setting

var_naming_pattern: "^[A-Z_][A-Za-z0-9_]*$"

Add a variable to the role vars main.yml file

---
Register_DNS_Host_Record_Variable: dummyvalue

Run ansible-lint on the new role

ansible-lint /collections/mynamespace/network/roles/register_dns_host_record
Desired Behavior

No errors should be reported

Actual Behavior

var-naming[no-role-prefix]: Variables names from within roles should use register_dns_hostrecord as a prefix. (vars: Register_DNS_Host_Record_Variable)

Qalthos commented 4 months ago

The var-naming[no-role-prefix] rule checks that role variables start with the role name exactly, i.e. including case. This is intentional to ensure consistency and clarity of role variables.

I can see in this case why you might want to use different casing, even if the prefix is otherwise the same, but we do not presently support this and are not planning on working on it at present. However, we do always welcome community contributions, and if you put together a PR, we will absolutely consider it.

cidrblock commented 4 months ago

Let's talk about this one. I think we can expose the regex to the user.