Ansible变量名称`environment`是保留的吗?

时间:2022-08-09 23:51:20

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 *es with defined vars.

但是,如果我尝试使用{{environment}}从脚本访问它,则始终会为其分配值{}。我改名了,它运作正常。这是为什么?环境是Ansible的保留名称吗?我检查了变量转储 - 没有定义变量的冲突。

2 个解决方案

#1


I found a statement regarding environment in the Google group:

我在Google小组中找到了有关环境的声明:

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.

所以它将删除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).

我也坚持使用不同的名称(尽管我仍然故意使用Environment来提醒自己变量名称的区分大小写)。

#1


I found a statement regarding environment in the Google group:

我在Google小组中找到了有关环境的声明:

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.

所以它将删除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).

我也坚持使用不同的名称(尽管我仍然故意使用Environment来提醒自己变量名称的区分大小写)。