You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using yaml serializer inside jinja template results in unicode being prepended by '!!python/unicode' #30454
My vote is still to just create another filter for safe_dump. I mean, I'd probably vote to just make the existing filter safe_dump, but that could break existing setups.
I ran into a cousin of this issue (in 2017.7.2) when serializing a Jinja variable into a state parameter:
{% set oldstring = "oldstring" %}
{% set olding = 123 %}
{% set mystring = "%s:%d"|format(oldstring, oldint) %}
mystate:
file.managed:
...
- default:
parameter: {{ mystring }}
This gets rendered as parameter: u'oldstring:123', which fails in two ways: (1) pyyaml doesn't know how to parse u'. (2) As a result, the state fails to compile because pyyaml finds the : separator outside of a string.
It looks like all of Jinja's built-in string formatting functions promote everything to unicode. That includes at least join(), format(), and string(). ("string()" is at least documented to convert to unicode.) As a result, if I do any formatting in Jinja, I end up with broken substitutions like the above. My workaround was:
parameter: {{ mystring|yaml }}
This gets rendered as !!python/unicode "oldstring:123", which, while ugly, at least works.
I'm mentioning it here mostly so that the workaround will save someone some time and headache in the future.
Reacted by Vladimir Selin, Robin Thoni, Philipp Wollschlegel and Peter Mudd
Activity
basepi commentedon Jan 19, 2016
My vote is still to just create another filter for safe_dump. I mean, I'd probably vote to just make the existing filter safe_dump, but that could break existing setups.
basepi commentedon Jan 20, 2016
I added the filter really quick. I didn't have a chance to test it, so if you have a minute that would be awesome. #30481
favoretti commentedon Jan 21, 2016
Awesome, thank you, we will test and report back.
mdevreugd commentedon Jan 22, 2016
I quick patched the salt master code with #30481 to see if it works, and unfortunately it turns out it didn't:
favoretti commentedon Jan 22, 2016
@basepi I can't re-open the issue, but apparently
safe_dumpdoesn't fix the issue :(basepi commentedon Jan 23, 2016
Hmmm, I guess I was under the impression you guys had tried changing the existing filter and it had worked. Well, back to the drawing board.
hemebond commentedon Jul 10, 2016
What is the reason for not adding the python/unicode constructor to the SaltYamlSafeLoader class?
multani commentedon Jan 4, 2017
I'm facing the same problem here. I made a small patch which allows to deserialize from YAML these values properly, as suggested by @hemebond .
It also reverts the #30481 as it never worked in the first place.
pjcreath commentedon Nov 17, 2017
I ran into a cousin of this issue (in 2017.7.2) when serializing a Jinja variable into a state parameter:
This gets rendered as
parameter: u'oldstring:123', which fails in two ways: (1) pyyaml doesn't know how to parseu'. (2) As a result, the state fails to compile because pyyaml finds the:separator outside of a string.It looks like all of Jinja's built-in string formatting functions promote everything to unicode. That includes at least join(), format(), and string(). ("string()" is at least documented to convert to unicode.) As a result, if I do any formatting in Jinja, I end up with broken substitutions like the above. My workaround was:
This gets rendered as
!!python/unicode "oldstring:123", which, while ugly, at least works.I'm mentioning it here mostly so that the workaround will save someone some time and headache in the future.