Search code examples
ansibleansible-factsansible-template

Ansible throws error after variable content is copied into fact


I have variables, that contain strings with other template engine markers, which unfortunately are three curly braces. If I work with those variables directly, everything is fine. If I copy them into a fact in order to modify them afterwards, Ansible spits an error.

Example playbook:

- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    foo: "header/P-Asserted-Identity=<sip:{{ '{{{another_template_engine_string}}}' }}@127.0.0.1>"
  tasks:
    - debug:
        var: foo

    - set_fact:
        bar: "{{ foo }}"

    - debug:
        var: bar

The output of my ansible-playbook run is this:

PLAY [localhost] **************************************************************************************************************************************************

TASK [debug] ******************************************************************************************************************************************************
ok: [localhost] => {
    "foo": "header/P-Asserted-Identity=<sip:{{{another_template_engine_string}}}@127.0.0.1>"
}

TASK [set_fact] ***************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ******************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating 'header/P-Asserted-Identity=<sip:{{{another_template_engine_string}}}@127.0.0.1>'.
  Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: expected token ':', got '}'.
  String: header/P-Asserted-Identity=<sip:{{{another_template_engine_string}}}@127.0.0.1>"}

PLAY RECAP ********************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

How do I have to format the variable content so I can work with it directly from the var as well as in a fact?

Thanks for all hints.

P.S.: Ansible version:

ansible [core 2.11.1]
  config file = None
  configured module search path = ['/Users/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/user/.venv/ansible3/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/user/.venv/ansible3/bin/ansible
  python version = 3.9.10 (main, Jan 15 2022, 11:40:53) [Clang 13.0.0 (clang-1300.0.29.3)]
  jinja version = 3.0.1
  libyaml = False

Solution

  • Did you try !unsafe?

    Like this:

     vars:
        foo: !unsafe "header/P-Asserted-Identity=<sip:{{ '{{{anot...
    

    Docs