为什么 YAML 中的制表符有时有效,但有时无效?

为什么 YAML 中的制表符有时有效,但有时无效?

我们在某些 YAML 文件中使用了制表符:

host:   "foo"
port:   8011
p:      "bar"

出于某种原因,对于某些用户来说,运行相同剧本的相同版本的 Ansible 可以很好地处理这些文件,但对于其他用户来说,却抱怨“无效字符”:

ERROR! Syntax Error while loading YAML.
  found character '\t' that cannot start any token

The error appears to have been in '.../playbooks/roles/native-package/defaults/main.yml': line 1, column
8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


tmpdir: >-
       ^ here
There appears to be a tab character at the start of the line.

YAML does not use tabs for formatting. Tabs should be replaced with spaces.

For example:
    - name: update tooling
      vars:
        version: 1.2.3
#    ^--- there is a tab there.

Should be written as:
    - name: update tooling
      vars:
        version: 1.2.3
# ^--- all spaces here.

我并不是在问制表符是否比空格“更好”——但我想了解其中的不一致性……

答案1

某些 IDE 或扩展程序可能会读取或转换制表符为空格,或将空格转换为制表符。请检查每个用户的文件中显示的制表符设置。

相关内容