1

In the following playbook, I set a fact my_var containing {{ undefined_variable }}:

- hosts:
    - localhost
  tasks:
    - name: Set my_var
      set_fact:
        my_var: "{{ '{{' }} undefined_variable {{ '}}' }}"
    - name: Dump my_var
      debug:
        msg: "dump: '{{ my_var }}'"

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.

I've tried with no luck to find a builtin filter that could indicate that the contents of the variable should not be interpolated.

Is there a way to do this?

  • 2
    You'll either see `my_var: VARIABLE IS NOT DEFINED!` or `fatal: ... 'undefined_variable' is undefined`. What do you expect? [edit] the question and make it [mre]. – Vladimir Botka Apr 02 '23 at 05:06
  • 2
    Hi, Welcome to SO, you may also benefit to read [how-to ask](https://stackoverflow.com/help/how-to-ask), the question seems to be an [XY problem](https://xyproblem.info/): it does not make clear what are you trying to accomplish, so it's very hard to provide a solution. – Carlos Monroy Nieblas Apr 02 '23 at 05:57
  • 2
    @CarlosMonroyNieblas I think it's not a XY problem: 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 }}`. – Ronald Higgins Apr 02 '23 at 12:45

1 Answers1

2

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 }}'''
U880D
  • 8,601
  • 6
  • 24
  • 40