ansiblejinja2

How to prevent Ansible to use Jinja2 templating?


I've encountered a very weird behaviour in Ansible 2.9.27. This code does not work:

- set_fact:
    toto: "{{ 'This is a valid string, even with these chars {{' }}"

- debug:
    msg: "I can't handle this variable anymore !"
  when: toto == toto

This seems obvious, but I can't choose the value of toto. I used set_fact to demonstrate the issue, but this value could come from any piece of code or from a file, a webservice, ...

In this code, the first task succeeds with the log message:

{
  "ansible_facts": {
    "toto": "This is a valid string, even with these chars {{"
  },
  "changed": false
}

but the second task crashes with (formatted for lisibility):

The conditional check 'toto == toto' failed.
The error was: An unhandled exception occurred while templating 
'This is a valid string, even with these chars {{'.
Error was a <class 'ansible.errors.AnsibleError'>,
original message: template error while templating string:
unexpected 'end of template'.
String: This is a valid string, even with these chars {{

The error appears to be in '/tasks/main.yml': line 6, column 3,
but may be elsewhere in the file depending on the exact syntax
problem.

The offending line appears to be:


- debug:
  ^ here

In short, any variable containing {{ substring cannot be used at all, because it seems that the Jinja templating is forced (even when we don't need it or even asked for it).

How to cope with this?

Thanks to @U880D suggestion, I tried to use the !unsafe pragma. However, this just pushes the problem back to when I define the variable.

Of course, if I try this, the code would work:

- set_fact:
    toto: !unsafe "{{ 'This is a valid string, even with these chars {{' }}"

But in a real world, I would populate the variable from a file. And if I try something like:

- set_fact:
    toto: !unsafe "{{ lookup('file', 'path/to/file.txt') }}"

Then the value of the variable is the lookup command, not the content of the file!


Solution

  • Based on Is there any way to prevent Ansible changing my JSON stored in a variable?

    What you need is a string. You have to explicitly declare it

    A minimal example playbook

    ---
    - hosts: localhost
      become: false
      gather_facts: false
    
      tasks:
    
        - set_fact:
            toto: "{{ lookup('file', 'file.txt') | string }}"
    
       - debug:
            var: toto | type_debug
    
        - debug:
            msg: "{{ toto }}"
    

    will result into an output of

    TASK [debug] *****************************************************
    ok: [localhost] =>
      toto | type_debug: NativeJinjaUnsafeText
    
    TASK [debug] ******************************************************
    ok: [localhost] =>
      msg: '{{ ''This is a valid string, even with these chars {{'' }}'