How can I print a jinja dict in a deterministic order?

Andrew

I have a configuration file where a particular setting takes a long ugly json-style dictionary. I would like to define this dictionary in YAML and print it out with Jinja. Currently this looks something like this:

{%- load_yaml as some_conf_val %}
val1: something
val2: completely
val3: different
{%- endload %}

option = {{ some_conf_val }}

# Results in:
option = {'val2': 'completely', 'val1': 'something', 'val3': 'different'}

By happy coincidence this is exactly the format expected by the program being configured, and the yaml block is much easier to read and modify than the inline version. However, the fact that the keys come out in neither alphabetical order nor the order they were defined leads me to suspect their output order is non-deterministic. On re-running the state a few times they always come out in the same order, but that doesn't prove much.

This is a problem because if the output string is altered, it gets treated as a change to the file and triggers a service restart, even if nothing functional has been changed. I don't care what specific order the keys are in, but I do need that order to be the same every time.

How can I accomplish this? Or is it already deterministic and just doesn't look like it?

(If I understand correctly jinja dicts are really python dicts under the hood, and python dicts are unordered, so this may be impossible without including code that's messier than the line I'm trying to not have to write. But I'm hoping not.)

Andrew

It turns out that saltstack provides a filter for json output that sorts its keys and can be massaged to get what I wanted. The solution I eventually used looks like this:

option = {{ some_conf_val|json|replace('"', "'") }}

# Results in:
option = {'val1': 'something', 'val2': 'completely', 'val3': 'different'}

The replace operation is because the option eventually gets fed to something that cares what kind of quotes it's looking at. It may not apply in other circumstances.

As far as I can tell the sorting behavior isn't documented, but you can find it in the source here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I use pickle to save a dict?

From Java

How can I import a json as a dict?

From Dev

How can I determine if a Jinja2 template block is empty?

From Dev

How can I print 413284921265094656?

From Dev

How can I break a for loop in jinja2?

From Dev

Print dict items in alphabet order

From Dev

CompositeDisposable - Deterministic order?

From Dev

How can I print a Maybe?

From Dev

How can I convert string to dict or list?

From Dev

jinja2: sort dict by defined key order?

From Dev

With Ansible how can I replace a variable with Jinja2

From Dev

How can I order this query?

From Dev

How can I comment in a Jinja directive?

From Dev

How can i print in descending order?

From Dev

Can I make SQL Server FORMAT deterministic?

From Dev

How can I use jinja2 to join with quotes in Ansible?

From Dev

How can I print every different word from an input on a different line sorted in an alphabetical order? - Java

From Dev

How can I print this?

From Dev

How can I print 413284921265094656?

From Dev

How to print dict string

From Dev

How can i rewrite this program so that it will print the value of text in reverse order

From Dev

How can I print items in an array in numerical order?

From Dev

How can I iterate over a dict which is indexed by a dict?

From Dev

how can i send a dict in data of ajax

From Dev

How do I print a dictionary on a single line in jinja2?

From Dev

How can I print only the keys of inner dictionaries that are nested within a list, within a dict, in alphabetical order?

From Dev

How can I serialize this Python dict?

From Dev

How can I parse such json dict?

From Dev

i can't print the linkedlist elements in reverse order using recursion

Related Related

HotTag

Archive