# Ansible-lint and the "role\_" prefix on variable names in collections

**URL:** <https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935>\
**Category:** Project Discussions\
**Tags:** fqcn, role, collections, ansible-lint\
**Created:** [December 15, 2023, 11:03pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935 "2023-12-15T23:03:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 15, 2023, 11:03pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/1 "2023-12-15T23:03:53Z")

</div>

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](http://forum.ansible.com/t/fully-qualified-tags/1720) 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.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 16, 2023, 4:10pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/2 "2023-12-16T16:10:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cavcrosby](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cavcrosby/32/1811_2.png) [@cavcrosby](https://forum.ansible.com/u/cavcrosby)\
**Post date:** [December 16, 2023, 10:05pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/3 "2023-12-16T22:05:38Z")

</div>

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](https://github.com/ansible/ansible-lint/discussions/3752#discussioncomment-7054836).

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 18, 2023, 2:40pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/4 "2023-12-18T14:40:11Z")

</div>

Hey @cavcrosby , welcome to _the forum_.

I refreshed my clone of the [ansible-lint](https://github.com/ansible/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. 🙂

---

<div class="post-metadata">

**Author:** ![falon](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/falon/32/2081_2.png) [@falon](https://forum.ansible.com/u/falon)\
**Post date:** [September 16, 2024, 9:48am UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/5 "2024-09-16T09:48:10Z")

</div>

> 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:

```auto
---
# 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:

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

```

test.yml:

```auto
---
- 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 ☹  
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

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 16, 2024, 9:52am UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/6 "2024-09-16T09:52:06Z")

</div>

You can specify what you want in `.ansible-lint` for example for a [PHP role I’ve written](https://git.coop/webarch/php) I have:

```yaml
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.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 16, 2024, 12:09pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/7 "2024-09-16T12:09:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 16, 2024, 12:14pm UTC](https://forum.ansible.com/t/ansible-lint-and-the-role-prefix-on-variable-names-in-collections/2935/8 "2024-09-16T12:14:02Z")

</div>

> [@utoddl](#):
>
> Where did you find that documented?

Either via a [previous mention on this forum](http://forum.ansible.com/t/role-var-param-naming-prefix/1855/3) or perhaps in a [ansible-lint issue](https://github.com/ansible/ansible-lint/issues), sorry I don’t recall where exactly. 🙄
