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

On Mon, 6 Nov 2023 at 16:04, matthew Higgs <[email protected]>
wrote:

> 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.
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4c276d28-e411-4ac5-b6b3-df42165d50b8n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/4c276d28-e411-4ac5-b6b3-df42165d50b8n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAF8BbLbQC%3Dr0rTCZgHer6fDwVmcTN12Zx8qawvbv%2BQdfoUiDcw%40mail.gmail.com.

Reply via email to