Skip to content

How Can I render Double Curly Braces `{{` and `}}` during Golang Template rendering?

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.

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.

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.

Example

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

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

Additional Information

Additional resources and information related to this Knowledge Base article.

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