ansible-lint icon indicating copy to clipboard operation
ansible-lint copied to clipboard

ansiblelint.rules.var_naming enforce role-name as variable prefix

Open SpikePy opened this issue 3 years ago • 2 comments

Summary

Allow/enable to check variable names if they have the role name as prefix

Issue Type
  • Bug Report/Feature Request .. not sure
STEPS TO REPRODUCE

create the following .ansible-lint file in your role

var_naming_pattern: "^{role}__[a-z0-9_]*$"

run ansible lint and it fails telling that it "violates variable naming standards"

Desired Behavior

set a variable that represents the role name inside the var_naming_pattern (as in the documentation of loop_var_prefix: "{role}_") to enforce prefixing every variable inside the role with the role name

Actual Behavior
var-naming: File defines variable 'k8s_grafana_users__url' that violates variable naming standards
defaults/main.yml:1

SpikePy avatar Apr 04 '22 13:04 SpikePy

Are you aware of https://github.com/ansible/ansible-lint/blob/main/.ansible-lint#L27 ?

If I am correct what you want is a new rule that would enforce roles to use only variables with unique prefixes, like role name?

ssbarnea avatar Apr 06 '22 12:04 ssbarnea

If I am correct what you want is a new rule that would enforce roles to use only variables with unique prefixes, like role name?

This is exactly what I want

SpikePy avatar Apr 06 '22 13:04 SpikePy

Has this gotten implemented? I'm hoping to use the logic that @SpikePy describes

mfann-or avatar Feb 23 '23 23:02 mfann-or

Now experiencing this new development, first thing I added var-naming[no-role-prefix] into my skip_list right away.

I understand when it checks the variables, that you set_fact yourself, but why does it check the vars, that are needed by other roles?

- name: invoking main of some other role
  ansible.builtin.include_role:
    name: some_other_role
  vars:
    variable_some_other_role_needs: "value of the variable some other role needs"

Why ansible-lint 6.16.2 complains with var-naming[no-role-prefix] about this variable_some_other_role_needs I gave to the other role, I haven't written that some_other_role. It's someone else's role, it's their problem. I'm just providing the vars that main.yml of some_other_role needs to work.

The rule var-naming[no-role-prefix] should only check what I create by set_fact or some other means the var is born.

EDIT: Oh, this complaint exists as a bug https://github.com/ansible/ansible-lint/issues/3465

MantvydasD avatar May 28 '23 11:05 MantvydasD