Inline vs template evaluation

I’ve run into what seems like a weird inconsistency in the way templates are evaluated vs inline variables.

I have a template file that contains this:

{{ '{{' }}.Name{{ '}}' }}

And the template is evaluated like this:

`

  • name: Evaluate template
    template:
    src: template.j2
    dest: /tmp/template.out

`

I get the following in /tmp/template.out, which is what I want and expect:

`
{{.Name}}

`

But if I now set the same string as a fact, like this:

`

  • name: Set fact
    set_fact:
    test1: “{{ ‘{{.’ }}Name{{ ‘}}’ }}”

`

and then try to write out the value of the fact, like this:

`

  • name: Write fact
    copy:
    content: “{{ test1 }}”
    dest: /tmp/test1.out

`

it fails with the following error:

`

TASK [ansible-role-parse-bug : Write fact] *************************************
fatal: [dockerhost]: FAILED! => {“failed”: true, “msg”: “{{.Name}}: template error while templating string: unexpected ‘.’. String: {{.Name}}”}

`

The same thing happens if I use a template file that just contains the variable.

It seems like there is some sort of double evaluation of the string going on when it’s set as the value of a fact. I’ve tried various permutations of quoting the fact string, but I can’t get it to work-- it seems to always get evaluated as a template and I haven’t found a way to have it treated as a string literal.

Is there a way to do this?

Did you try to define the variable to something like “{{.Name}}”?

I done some tests and I finally came which this terrible and ugly (but works) piece

of code:

  • hosts: localhost
    gather_facts: false
    tasks:
  • name: set fact
    set_fact:
    test1: “{{ lookup(‘vars’, ‘test2’, default=‘{{.Name}}’) }}”
  • name: debug
    debug:
    var: test1

The idea is since test2 is a non-existent variable, the default value is used.

Sorry for this solution. (: