# Literal scalar expands to a dictionary

**URL:** <https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173>\
**Category:** Ansible Project\
**Created:** [January 5, 2022, 10:21am UTC](https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173 "2022-01-05T10:21:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 5, 2022, 10:21am UTC](https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173/1 "2022-01-05T10:21:42Z")

</div>

For example, given "val1: foo"

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_var1: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"key1": "{{ val1 }}"}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_var2: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"key1": "foo"}  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: \_var1|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: \_var2|type\_debug

gives

&nbsp;&nbsp;\_var1|type\_debug: dict  
&nbsp;&nbsp;\_var2|type\_debug: AnsibleUnicode

\*\_var1\* expands to a dictionary but \*\_var2\* expands to a string. Is it  
a feature or a bug?

Thank you,

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 5, 2022, 4:09pm UTC](https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173/2 "2022-01-05T16:09:45Z")

</div>

#1 from this response explains why this happens:

[https://github.com/ansible/ansible/issues/34595#issuecomment-356091161](https://github.com/ansible/ansible/issues/34595#issuecomment-356091161)

If the value contains a variable, it is processed through Templar.template  
a. Templar.template is powered by jinja2, and historically only has the ability to return strings  
b. Because jinja2 has historically only had the ability to template strings, we do some magic to try and convert strings that look like python data structures to python data structures  
c. The string now becomes a python dictionary

You can work around this, although it’s a bit more complex:

- set\_fact:  
\_var1: |  
{{ (‘{“key1”: "’ ~ val1 ~ ‘"}’) | string }}

See [https://docs.ansible.com/ansible-core/2.12/reference\_appendices/config.html#string-type-filters](https://docs.ansible.com/ansible-core/2.12/reference_appendices/config.html#string-type-filters)

There are likely some other variants on my example.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 5, 2022, 4:24pm UTC](https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173/3 "2022-01-05T16:24:34Z")

</div>

Why don't you try this magic consistently both on jinja2 and  
non-jinja2 expressions?

Thank you,

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 5, 2022, 7:38pm UTC](https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173/4 "2022-01-05T19:38:18Z")

</div>

I believe that was explained. The “magic” as you call it, only affects jinja2 templating, because historically jinja2 could only ever generate strings, but the intention was often to generate python data types.

We don’t do that for things that don’t go through templating, because the author can define it explicitly as the type they want.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 5, 2022, 10:58pm UTC](https://forum.ansible.com/t/literal-scalar-expands-to-a-dictionary/36173/5 "2022-01-05T22:58:42Z")

</div>

Thank you. I try to understand the logic. In summary (up till now),

&nbsp;&nbsp;"The magic only affects jinja2 templating; to try and convert  
&nbsp;&nbsp;strings that look like python data structures to python data  
&nbsp;&nbsp;structures."

How do you decide what looks like python data structure? For example,  
only the second string below is converted to dictionary, despite the  
fact that both are valid YAML dictionaries

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_var3: '{"key1": {{ val1 }}}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_var4: '{"key1": "{{ val1 }}"}'  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: \_var3|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: \_var3|from\_yaml|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: \_var4|type\_debug

gives

&nbsp;&nbsp;\_var3|type\_debug: str  
&nbsp;&nbsp;\_var3|from\_yaml|type\_debug: dict  
&nbsp;&nbsp;\_var4|type\_debug: dict
