20.26. kb-00025: How Can I render Double Curly Braces {{ and }} during Golang Template rendering?

20.26.1. Knowledge Base Article: kb-00025

20.26.2. Description

Often times, content based Golang templating is used to generate template files used by other systems on the target machine (eg Jinja, Ansible, etc.). Many of these templating systems also use double curly braces to signal to the templating engine to perform expansion and advanced rendering capabilities.

In these cases, the content author must arrange the DRP Golang based template to protect the double curly braces from being intepretted by dr-provision at render time, so they are served as in-tact double curly braces to the target Machine.

Note that this solution is NOT an exhaustive reference of how to do this, but does describe some possible techniques.

20.26.3. Solutions

Golang template complains if you are building a template that also uses double curly brances (e.g. {{ jinja 2 for ansible }}) and it can be difficult to properly escape these characters.

There are multiple solutions, please review to see which best fits your needs.

Solution 1: Escape In Place

You can also use a pair of single quotes (`) to escape the block in place to create an string that is not modifed by gotemplates inside a gotemplate brace pair.

Generallly, this approach is recommended because it is simple and easy to read.

For example:

"{{`{{ services['drpcli.service'].status }}`}}"

Solution 2: Substitution

Another workaround is to define variables for the double brace and then use the variable in the template.

This approach is needed to make more template substitions _inside_ the Ansible code block.

For example:

{{ $openblock := "\x7B\x7B" }}
{{ $closeblock := "\x7D\x7D" }}

- hosts: localhost
    braceme: "{{ $openblock }}{{ .Param "variable" }}{{ $closeblock }}"

20.26.4. Additional Information

Additional resources and information related to this Knowledge Base article.

Example from codebase: https://gitlab.com/rackn/provision-content/blob/521f3ed31242762dba04699f51f764949ec10fb3/task-library/tasks/ansible-join-up.yaml#L60 See Also Versions

all Keywords

double curly braces, jinja2, ansible, rendering, template Revision Information

KB Article     :  kb-00025
initial release:  Wed Jun 10 12:03:02 PDT 2020
updated release:  Wed Jun 10 12:03:02 PDT 2020