Ansible-lint and the "role_" prefix on variable names in collections

We’ve always been pretty consistent about prefixing all role variables with the role name followed by an underscore. For example the mw_common_zmaint role’s server_url variable is spelled mw_common_zmaint_server_url.

Now we’re moving some of our “common” roles like mw_common_zmaint into our “common collection” collection, hence the collection’s FQCN mw.cc. We’re also dropping the mw_common_ from the stand-alone roles’ names, so this role becomes mw.cc.zmaint.

That mw.cc FQCN prefix works great on the collection’s plugins (modules, tests, filters, lookups, etc.), but not on variables. For example mw.cc.zmaint_server_url, while unambiguous, is not allowed in current Ansible syntax. (I’ve suggested elsewhere that more “stuff” should use FQCNs, but that’s a stronger assertion than I’m making here.)

Given that we can’t FQCN-ify variables (yet!), the next best thing would be to prefix them unambiguously. In this case that means spelling it mw_cc_zmaint_server_url. And that works pretty well for all concerned parties …

… except ansible-lint, which throws the following shade:

collections/ansible_collections/mw/cc/roles/zmaint/defaults/main.yml:4: var-naming[no-role-prefix]: Variables names from within roles should use zmaint_ as a prefix. (vars: mw_cc_zmaint_server_url)

I’m not claiming ansible-lint is wrong in this case. But I do think it should accept either a bare rolename_ prefix, or a more fully qualified namespace_collection_rolename_ variable prefix.

3 Likes

Makes sense to me, incidently I think ansible-lint seems to be fine with rolename prefixes — I don’t thinking the underscore is required, at least I have some roles that a variable with same name as the role name and others that start with the role name but are not followed by an underscore and there are no warnings for either of these.

I think the maintainers of ansible-lint would be opened to a PR that would allow this prefix to be configurable. Though, I’m going based on some previous release discussion v6.20.0 · ansible/ansible-lint · Discussion #3752 · GitHub.

2 Likes

Hey @cavcrosby , welcome to the forum.

I refreshed my clone of the ansible-lint project, thinking this should be an easy one. It’s certainly easy enough to see where the check is made and the message generated, but how to add context wrt collection name space and roles wasn’t particularly obvious. ansible-lint has gotten really clever, which means you’ve got to stare at the code a while to really understand how it works.

tl;dr - I may be adding “# noqa” for a while before I figure out how to make this lint cleanly without it.

If someone who already understands ansible-lint’s inner quirks wants to, feel free to toss suggestions over the wall. :slight_smile:

1 Like

I don’t thinking the underscore is required, at least I have some roles that a variable with same name as the role name

Hello,

time ago it was as described above. Unfortunately with latest version of ansible-lint (24.9.0) this is no longer true.

Ansible-lint allows me to write a role with a dict which is the role name. For example, I write a collection with:

---
# role_name: myrole
# path: roles/myrole/defaults/main.yml
# defaults file for role myrole

myrole:
  path: /usr/alice
  home: /usr/share/bob

and the ansible-lint on the role doesn’t claim.

But when I write a play which tries to redefine the default var I see:

var-naming[no-role-prefix]: Variables names from within roles should use myrole_ as a prefix. (vars: myrole)
tests/test.yml:8

test.yml:

---
- name: Create new path
  hosts: myhosts
  roles:
    - role: namespace.mycollection.myrole
      vars:
        myrole:
          path: /root/myplace

ansible-lint 24.9.0 using ansible-core:2.16.9 ansible-compat:24.9.0 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8

The possibility to write role with a dict with the role name was nice :frowning:
Really, if I must write vars with the underscore, then the violation should be present also during the linting of the role and not only in a playbook.

Kind Regards
Marco

You can specify what you want in .ansible-lint for example for a PHP role I’ve written I have:

var_naming_pattern: "^[php|molecule]_?[a-z0-9_]*$"

So I can use php as a variable as well as php_foo and php_bar and also have separate variables names allowed for the Moulcule tests.

1 Like

That’s really neat, @chris . Where did you find that documented? I don’t see it on home - Ansible Lint Documentation (which sports those shiny new 2023 copyright notices which always inspires such confidence).

1 Like

Either via a previous mention on this forum or perhaps in a ansible-lint issue, sorry I don’t recall where exactly. :roll_eyes: