According your description
I'd like to be able to use this fact (eg. in the debug
task, but the problem occurs every time {{ my_var }}
is used) as a raw string, without having its contents interpolated ... the contents of the variable should not be interpolated.
and
I set somewhere in my task a fact (in the example my_var
) whose value may contain a literal {{ x }}
, and I'd like to print it literally using debug
later (the output should be "{{ x }}
"). But debug
seems to interpret the {{ x }}
.
it seems that you need to mark your variable as unsafe
and as described in Advanced playbook syntax - Unsafe or raw strings
... The most common use cases include passwords that allow special characters like {
or %
, and JSON arguments that look like templates but should not be templated ...
To do so, use
my_var: !unsafe "{{ undefined_variable }}"
Similar Q&A (somehow)
Further Documentation
Minimal Reproducible Example
A playbook
- hosts: localhost
become: false
gather_facts: false
vars:
undef_1: "{{ '{{' }} undefined_variable {{ '}}' }}"
tasks:
- name: Set Fact
set_fact:
undef_2: "{{ '{{' }} undefined_variable {{ '}}' }}"
- name: Show from vars definition
debug:
msg: "dump: '{{ undef_1 }}'"
- name: Show from set_fact
debug:
msg: "dump: '{{ undef_2 }}'"
ignore_errors: true
- name: Set Fact !unsafe
set_fact:
undef_3: !unsafe "{{ undefined_variable }}"
- name: Show from unsafe set_fact
debug:
msg: "dump: '{{ undef_3 }}'"
will result into an output of
TASK [Set Fact] **********************************************************************************************************************
ok: [localhost]
TASK [Show from vars definition] *****************************************************************************************************
ok: [localhost] =>
msg: 'dump: ''{{ undefined_variable }}'''
TASK [Show from set_fact] ************************************************************************************************************
fatal: [localhost]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: {{ undefined_variable }}: 'undefined_variable' is undefined
The error appears to be in '/home/ansible-user/test/unsafe.yml': line 20, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Show from set_fact
^ here
...ignoring
TASK [Set Fact !unsafe] **************************************************************************************************************
ok: [localhost]
TASK [Show from unsafe set_fact] *****************************************************************************************************
ok: [localhost] =>
msg: 'dump: ''{{ undefined_variable }}'''