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 We’re also dropping the mw_common_ from the stand-alone roles’ names, so this role becomes

That FQCN prefix works great on the collection’s plugins (modules, tests, filters, lookups, etc.), but not on variables. For example, 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.


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.


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