Ansible如何准确解析布尔变量? [英] How Exactly Does Ansible Parse Boolean Variables?
问题描述
在Ansible中,可以在多个地方定义变量:在清单中,在剧本中,在变量文件中,等等.有人可以解释我的以下发现吗?
In Ansible, there are several places where variables can be defined: in the inventory, in a playbook, in variable files, etc. Can anyone explain the following observations that I have made?
- 在清单中定义布尔变量时,必须将其大写(即True/False),否则(即true/false)将不会被解释为布尔值,而是字符串.
- 在任何YAML格式的文件(剧本,角色等)中,True/False和true/false均被解释为布尔值.
例如,我在清单中定义了两个变量:
For example, I defined two variables in an inventory:
abc=false
xyz=False
在调试角色中的这些变量的类型时...
And when debugging the type of these variables inside a role...
- debug:
msg: "abc={{ abc | type_debug }} xyz={{ xyz | type_debug }}"
...然后abc
变为unicode
,但xyz
被解释为bool
:
... then abc
becomes unicode
but xyz
is interpreted as a bool
:
ok: [localhost] => {
"msg": "abc=unicode xyz=bool"
}
但是,在剧本中定义相同的变量时,如下所示:
However, when defining the same variables in a playbook, like this:
vars:
abc: false
xyz: False
...则两个变量都被识别为bool
.
... then both variables are recognized as bool
.
在生产上执行一本剧本之后,由于库存中的变量设置为"false"而不是"False",因此我不得不意识到这是很难的事情.因此,我真的很想找到一个清晰的答案,以了解Ansible如何理解布尔值以及它如何取决于变量的定义位置/方式.为了安全起见,我是否应该总是只使用大写的True/False?是否可以说YAML文件(格式为key: value
)中的布尔值不区分大小写,而属性文件(格式为key=value
)中的布尔值则区分大小写?任何更深刻的见解将不胜感激.
I had to realize this the hard way after executing a playbook on production, running something that should not have run because of a variable set to 'false' instead of 'False' in an inventory. Thus, I'd really like to find a clear answer about how Ansible understands Booleans and how it depends on where/how the variable is defined. Should I simply always use capitalized True/False to be on the safe side? Is it valid to say that booleans in YAML files (with format key: value
) are case-insensitive, while in properties files (with format key=value
) they are case-sensitive? Any deeper insights would be highly appreciated.
推荐答案
在YAML文件中定义的变量(剧本,vars_files和YAML格式的广告资源)
YAML原则
首先,由YAML解析器处理以YAML编写的剧本,vars_files和库存文件.它为值存储了Boolean
类型提供了几种别名:yes
/no
,true
/false
,on
/off
,在多种情况下定义:true
/True
/TRUE
(因此它们并不区分大小写).
Variables defined in YAML files (playbooks, vars_files, YAML-format inventories)
YAML principles
Playbooks, vars_files, and inventory files written in YAML are processed by a YAML parser first. It allows several aliases for values which will be stored as Boolean
type: yes
/no
, true
/false
, on
/off
, defined in several cases: true
/True
/TRUE
(thus they are not truly case-insensitive).
YAML定义将可能的值指定为:
y|Y|yes|Yes|YES|n|N|no|No|NO
|true|True|TRUE|false|False|FALSE
|on|On|ON|off|Off|OFF
您还可以以几种形式指定布尔值(真/假):
You can also specify a boolean value (true/false) in several forms:
create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false
以INI格式的清单文件定义的变量
Python原理
当Ansible读取INI格式的广告资源时,它会处理变量使用Python内置类型:
Variables defined in INI-format inventory files
Python principles
When Ansible reads an INI-format inventory, it processes the variables using Python built-in types:
使用
key=value
语法传递的值将解释为Python文字结构(字符串,数字,元组,列表,字典,布尔值,无)或字符串.例如,var=FALSE
将创建一个等于FALSE
的字符串.
Values passed in using the
key=value
syntax are interpreted as Python literal structure (strings, numbers, tuples, lists, dicts, booleans, None), alternatively as string. For examplevar=FALSE
would create a string equal toFALSE
.
如果指定的值与字符串True
或False
(以大写字母开头)匹配,则将该类型设置为布尔值,否则将被视为字符串(除非与其他类型匹配).
If the value specified matches string True
or False
(starting with a capital letter) the type is set to Boolean, otherwise it is treated as string (unless it matches another type).
在CLI中作为Extra-vars传递的所有变量均为字符串类型.
All variables passed as extra-vars in CLI are of string type.
这篇关于Ansible如何准确解析布尔变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!