#1 from this response explains why this happens:
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
There are likely some other variants on my example.
On Wed, Jan 5, 2022 at 4:21 AM Vladimir Botka <[email protected]> wrote:
> For example, given "val1: foo"
>
> - set_fact:
> _var1: |
> {"key1": "{{ val1 }}"}
> _var2: |
> {"key1": "foo"}
> - debug:
> var: _var1|type_debug
> - debug:
> var: _var2|type_debug
>
> gives
>
> _var1|type_debug: dict
> _var2|type_debug: AnsibleUnicode
>
> *_var1* expands to a dictionary but *_var2* expands to a string. Is it
> a feature or a bug?
>
> Thank you,
>
> --
> Vladimir Botka
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/20220105112142.22faded9%40gmail.com
> .
>
--
Matt Martz
@sivel
sivel.net
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAD8N0v-xGpxF5i4aT1EXx_QRSNNbaaF03ts651B3SrChCqJb8Q%40mail.gmail.com.