Ansible 内置模板无法使用整数值创建有效的 yaml 结果

Ansible builtin template unable to create valid yaml result with integer values

我想用 ansible 模板 .yaml 文件。我假设这是一项标准工作,但后来我意识到模板模块无法在 YAML 输出中生成有效整数。

给定库存变量 bar 设置为 1,使用以下模板:

foo: "{{ bar | int }}"

和一个非常基本的剧本,只是做模板。这样做,我最终得到

foo: "1"

在生成的 .yaml 文件中是错误的。根据文档,结果应该是

foo: 1

我正在使用 ansible 2.10.5

这里有什么问题?

我意识到,我必须像对待模板一样对待模板:模板。

我通过编写一个填充模板并对结果进行检查的测试来解决这个问题。这需要多做一些工作,但可以确保有效的模板结果。

在您的 .j2 模板中,您不需要引用您的表达式。

These quotes are part of your template and will be part of the rendered output.

...

Everything that is not inside a jinja2 delimited block ({%, {{, etc.) will translate to the rendered value.

所以你可以这样写:

foo: {{ bar }}