# Inline vs template evaluation

**URL:** <https://forum.ansible.com/t/inline-vs-template-evaluation/27920>\
**Category:** Ansible Project\
**Created:** [June 29, 2018, 9:07pm UTC](https://forum.ansible.com/t/inline-vs-template-evaluation/27920 "2018-06-29T21:07:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![durist](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@durist](https://forum.ansible.com/u/durist)\
**Post date:** [June 29, 2018, 9:07pm UTC](https://forum.ansible.com/t/inline-vs-template-evaluation/27920/1 "2018-06-29T21:07:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mhalano](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mhalano/32/1815_2.png) [@mhalano](https://forum.ansible.com/u/mhalano)\
**Post date:** [July 2, 2018, 5:48pm UTC](https://forum.ansible.com/t/inline-vs-template-evaluation/27920/2 "2018-07-02T17:48:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mhalano](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mhalano/32/1815_2.png) [@mhalano](https://forum.ansible.com/u/mhalano)\
**Post date:** [July 2, 2018, 6:46pm UTC](https://forum.ansible.com/t/inline-vs-template-evaluation/27920/3 "2018-07-02T18:46:22Z")

</div>

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. (:
