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:
---
- name: test
hosts: all
user: root
vars:
myhost: my.host.name
vars_files:
# defines xxx.yyydefault value
- vars.yml
# overrides xxx.yyy(I thought! Apparently it cannot.)
- "{{inventory_hostname}}.yml"
tasks:
- debug: msg="my var is {{ xxx.yyy }}"
This playbook uses two included vars files, vars.yml and
my.host.name.yml (where the playbook is being applied to a host named
"my.host.name" in the inventory).
I had expected the order of inclusion to define the precedence, in the
absence of any other information in the documentation:
http://docs.ansible.com/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
However, whatever order I used, vars.yml won. It wasn't a mistake in my
naming, as
- If a variable was named in my.host.name.yml but not vars.yml, that
got set fine.
- If I removed vars.yml from the includes entirely, xxx.yyy got set
from my.host.name.yml fine.
Interestingly, "{{myhost}}.yml" still wins over
"{{inventory_hotname}}.yml", when "myhost" is set to "my.host.name", and
the order still doesn't seem to affect this.
To conclude, if I'm right, there seems to be an implicit order of
preference which means that paths constructed with "inventory_hostname"
(and maybe other built-in variables?) are evaluated first, but later
includes override them. Apparently it isn't possible to override
non-host specific vars depending on the host this way. To be clear, I
know there are other ways including using host_vars, the include task
module, etc., but that's a different point and not what I want to ask about.
The documentation seems to have nothing to say on this behaviour, so I
thought I'd:
a) point that out, assuming it is intentional...
but also:
b) ask for clarification of the details, and whether it really is
intentional, since it seems to preclude overriding vars as in this use case?
Are the are any other special variables?
What precedence is intended when ordinary vars like "myhost" are used
instead of ones like "inventory_host"?
c) ask if is it something which may be changed at a later date, or
relied on not to change?
Thanks,
Nick
---
ansible --version
ansible 1.8 (devel 2eda9a3a47) last updated 2014/10/10 18:43:36 (GMT +100)
lib/ansible/modules/core: (detached HEAD 5af8d55b03) last updated
2014/10/10 18:43:44 (GMT +100)
lib/ansible/modules/extras: (detached HEAD 681db4ce2c) last updated
2014/10/10 18:43:45 (GMT +100)
v2/ansible/modules/core: (detached HEAD cb69744bce) last updated
2014/10/08 10:13:54 (GMT +100)
v2/ansible/modules/extras: (detached HEAD 8a4f07eecd) last updated
2014/10/08 10:14:38 (GMT +100)
configured module search path = library/modules
--
You received this message because you are subscribed to the Google Groups "Ansible
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/544E8DB4.8010308%40letterboxes.org.
For more options, visit https://groups.google.com/d/optout.