Ansible variable name `environment` is reserved?

岁酱吖の 提交于 2021-02-16 16:11:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!