问题描述
在 Ansible 中,有几个地方可以定义变量:在清单中、在剧本中、在变量文件中等等.谁能解释一下我所做的以下观察?
- 在清单中定义布尔变量时,它必须大写(即 True/False),否则(即 true/false)它不会被解释为布尔值,而是解释为字符串.
- 在任何 YAML 格式的文件(剧本、角色等)中,真/假和真/假都被解释为布尔值.
例如,我在一个清单中定义了两个变量:
abc=falsexyz=假
并且在调试角色内部这些变量的类型时...
- 调试:msg: "abc={{ abc | type_debug }} xyz={{ xyz | type_debug }}"
... 然后 abc
变成 unicode
但 xyz
被解释为 bool
:
ok: [localhost] =>{"msg": "abc=unicode xyz=bool"}
但是,在剧本中定义相同的变量时,如下所示:
变量:abc:假xyz:错误
... 那么这两个变量都被识别为 bool
.
在生产中执行剧本后,我不得不以艰难的方式意识到这一点,运行一些不应该运行的东西,因为库存中的变量设置为false"而不是False".因此,我真的很想找到关于 Ansible 如何理解布尔值以及它如何取决于变量的定义位置/方式的明确答案.为了安全起见,我应该总是使用大写的 True/False 吗?可以说 YAML 文件中的布尔值(格式为 key: value
)不区分大小写,而在属性文件中(格式为 key=value
)它们是大小写的吗?-敏感的?任何更深入的见解将不胜感激.
YAML 文件中定义的变量(剧本、vars_files、YAML 格式的清单)
YAML 原则
Playbooks、vars_files 和 用 YAML 编写的清单文件首先由 YAML 解析器处理.它允许将值存储为 Boolean
类型的多个别名:yes
/no
, true
/false
、on
/off
,定义在几种情况下:true
/True
/TRUE
(因此它们并非真正不区分大小写).
YAML 定义 将可能的值指定为:
y|Y|yes|Yes|YES|n|N|no|No|NO|真|真|真|假|假|假|开|开|开|关|关|关
您还可以以多种形式指定布尔值(真/假):
create_key: 是需要代理:没有know_oop: 真likes_emacs:真uses_cvs: 假
INI 格式清单文件中定义的变量
Python 原理
当 Ansible 读取 INI 格式的清单时,它会处理变量 使用 Python 内置类型:
使用 key=value
语法传入的值被解释为 Python 文字结构(字符串、数字、元组、列表、字典、布尔值、无),或者作为字符串.例如 var=FALSE
将创建一个等于 FALSE
的字符串.
如果指定的值与字符串 True
或 False
(以大写字母开头)匹配,则类型设置为布尔值,否则将其视为字符串(除非匹配另一种类型).
通过--extra_vars
CLI 参数定义的变量
所有字符串
在 CLI 中作为 extra-vars 传递的所有变量都是字符串类型.
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?
- When defining a Boolean variable in an inventory, it MUST be capitalized (i.e., True/False), otherwise (i.e., true/false) it will not be interpreted as a Boolean but as a String.
- In any of the YAML formatted files (playbooks, roles, etc.) both True/False and true/false are interpreted as Booleans.
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 }}"
... 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
... then both variables are recognized as bool
.
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.
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 definition specifies possible values as:
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:
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).
Variables defined through --extra_vars
CLI parameter
All strings
All variables passed as extra-vars in CLI are of string type.
这篇关于Ansible 解析布尔变量的准确程度如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!