Issue escaping curly braces in template file

546 views
Skip to first unread message

matthew Higgs

unread,
Nov 6, 2023, 10:04:12 AM11/6/23
to Ansible Project
Hey.  I am having a bit of trouble trying to convert a jinja template because of the some issues escaping curly braces.  Consider this simple jinja template that is currently returning an error:

activationID: {{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') }}}
CustomerID: {{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') }}}

As you can see, there is an environment variable storing a string value that I need, but the final string that my playbook needs is that value surrounded by single curly brackets, eg:

{34-34-456-34}

I have tried various methods I found online to attempt to get ansible to properly convert the jinja template, including:

Example 1:
activationID: \{{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') }}\}
CustomerID: \{{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') }}\}

Example 2:
activationID: "{{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') }}}"
CustomerID: "{{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') }}}"

Example 3:
activationID: "\{{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') }}\}"
CustomerID: "\{{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') }}\}"

But no matter what I try, I keep getting this error:

The error was: . expected token ':', got '}
I did notice some interesting output from ansible regarding how the template is being parsed as I ran the examples listed above.  For example, the output when I ran example 1:
String: activationID: \\{{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') }}\\}\nCustomerID: \\{{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') }}\\}\n. expected token ':', got '}'"
As you can see, "\" which I want to escape the outer most curly brace is itself being automatically escaped by ansible.   Same when I run example 3:

"msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: activationID: \"\\{{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') }}\\}\"\nCustomerID: \"\\{{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') }}\\}\"\n. expected token ':', got '}'"

I am at a loss as to what to do here.  Any help would be awesome.

Todd Lewis

unread,
Nov 6, 2023, 10:32:09 AM11/6/23
to ansible...@googlegroups.com, uto...@gmail.com
Maybe try these expressions:
{{ '{' ~ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') ~ '}' }}
{{ '{' ~ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') ~ '}' }}
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4c276d28-e411-4ac5-b6b3-df42165d50b8n%40googlegroups.com.

-- 
Todd

Dick Visser

unread,
Nov 6, 2023, 1:09:12 PM11/6/23
to ansible...@googlegroups.com
If you need this a lot then you could even dedicate a filter to it.
For example, create a directory called 'filter_plugins' adjacent to your playbook and then create a file called 'filters.py' that contains:

def embrace(string):
    return '{' + string + '}'
class FilterModule(object):
    def filters(self):
        return {
            'embrace': embrace
        }


Now you can do:

activationID: "{{ lookup('ansible.builtin.env', 'QUALYS_ACTIVATIONID') | embrace }}"
CustomerID: "{{ lookup('ansible.builtin.env', 'QUALYS_CUSTOMERID') | embrace }}"

Dick

--
Reply all
Reply to author
Forward
0 new messages