Group vars override role vars?

I have a role defined with:

`

  • hosts: elk
    sudo: true
    vars:
    es_heap: 8G

`

I run that role against a given inventory that attempts to override that variable:

`
[elk]
node1 es_heap=2G

[elk:vars]
es_heap=2G

`

Tried overriding with both host and group vars, but I keep getting the role variable. Aren’t the former supposed to override the latter? I also tried moving the variable from the role in /vars/main.yml, but that had no effect. I must be missing something, because having host variable override role variable seems pretty basic.

no, they override the role's defaults/main.yml, but not vars/main.yml

Put it into the role's defaults (roles/foo/defaults/main.yml). Those
are the ones that are really meant to be overridden.

Ah thanks, that makes more sense.

I’m not sure why I’d ever want to use vars/ then. Sure I could be convinced of a use-case, but it seems much more natural for inventory/group/host vars to override anything defined in a role, but I see this discussion has taken place before. :slight_smile:

I use group_vars a lot and it’s fine to do so.

It is useful for declaring something like a constant though, to save typing, or make things configured in one specific place.

(It keeps configuration of that thing with the playbook vs the inventory)

I’m not sure why I’d ever want to use vars/ then. Sure I could be
convinced of a use-case

A use case I've seen, which doesn't seem all that uncommon, is when you
want to use a given role with multiple inventories, each of which has its
own inventory/group/host vars structure. Putting the default in the role
lets you have it in one place, and be confident that it's consistent
across your various inventories unless you explicitly override it.

                                      -Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

Role variables are definitely a thing.

Where you can move top level playbook things to vars/ or defaults/ in a role it can be a good idea.