问题
I've got a strange situation today. I called one of my variables in a playbook environment
. If I dump variables using debug
instruction, it is shown ok
"environment": "development",
But if I try to access it from a script using {{ environment }}
it is always assigned a value of {}
. I renamed it and it worked ok. Why is that? Is environment
a reserved name in Ansible? I checked variable dump - there is no clashes with defined vars.
回答1:
I found a statement regarding environment
in the Google group:
Yes, environment is a restricted keyword. Initially specific within playbooks, but it gets injected in the runner:
inject['environment'] = self.environment
so it will erase whatever value was set by inventory of vars.
There is a yet unaddressed feature request to warn if reserved variables are used.
回答2:
Interestingly enough, Environment
(proper-case) is usable, but environment
(lower-case) is not as you found out.
I came across this one day when I accidentally used my existing Environment variable specified in lower-case and had a fun time trying to diagnose the issue.
I'd stick to using a different name as well (although I still use Environment
on purpose just to remind myself about case sensitivity in variable names).
来源:https://stackoverflow.com/questions/30014136/ansible-variable-name-environment-is-reserved