the precedence of var_files includes when path is constructed from vars

Hello,

Something I discovered about ansible this morning surprised me. Essentially: when loading vars files, anything loaded with a *dynamically constructed name depending on the host* seems to be lower precedence than those files with fixed names, or even dynamic names not depending on the host. I'd have expected the opposite? (My ansible version is a recent development one, specific information appended below.)

To elaborate, I was trying to override some variables like this:

For the record, currently I am working around the aforementioned var precedence behaviour with a diabolical hack, as follows:

inventory scoped variables have less priority than globally scoped things.

This is expected, yes.

Thanks for confirming. This case didn't appear to be inventory scoped, as it
wasn't taking information from host_vars or group_vars, but I see why it is
logically the same.
Even so, the precedence seems backward: less specific globals beat more specific
inventory vars. Is there a reason for this or is it purely historical? If you
can tell me I'll attempt a documentation patch.

Assuming I'm not a weirdo and it is indeed a common case to want global defaults
which can be over-ridden on specific hosts, then I don't see a way to do that at
a playbook level rather than using role defaults or follownig the docs'
recommendation that "Site wide defaults should be defined as a ‘group_vars/all’
setting". These both force me to move playbook specific information into a
location where they lose context or may be swamped by unrelated definitions.
Task includes and the set_fact module might be another option, but they set vars
at "task time", which I believe is too late for the values to be seen in roles
the playbook might include.

Would playbook default vars be considered a reasonable feature request?

N

I’m happy with things as documented today.

Please refer to

Variables — Ansible Documentation

We have several thousands of users here and can’t change behavior based on a particular request to do it differently, unfortunately.

If you want defaults, look at “defaults” in roles, which have lower power than even inventory.