2

I want to template .yaml files with ansible. This is a standard job, I assumed, but then I realized that the template module is unable to produce valid integers in the YAML output.

Given an inventory var bar is set to 1, using the following template:

foo: "{{ bar | int }}"

and a very basic playbook, just doing the template. Doing so, I end up with

foo: "1"

in the resulting .yaml file which is wrong. According to the docs, the result should be

foo: 1

I am using ansible 2.10.5

What is the problem here?

moestly
  • 1,681
  • 15
  • 19

2 Answers2

0

I realized, that I have to treat a template as what it is: a template.

I worked around this by writing a test that fills the template and lints the result. This is a little more work, but ensures valid template results.

moestly
  • 1,681
  • 15
  • 19
0

At your .j2 template, you don't need to quote your expression.

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.

https://stackoverflow.com/a/61685601/6346520

So you can write:

foo: {{ bar }}
kwinkel
  • 177
  • 2
  • 10