Sorted the dictionary.

This commit is contained in:
Jeff Baskin 2017-01-18 21:29:15 -05:00
parent f96a5b9b4b
commit 243ab902ab
2 changed files with 12 additions and 5 deletions

View File

@ -1,3 +1,5 @@
{%- set key = 0 -%}
{%- set value = 1 -%}
{%- macro keepalived_config(data, recurse=-1) -%}
{%- set recurse = recurse + 1 -%}
{%- if data is none -%}
@ -8,16 +10,16 @@
{%- if recurse > 0 -%}
{{- '{\n' -}}
{%- endif -%}
{%- for key, value in data.iteritems() -%}
{{- key }} {{ keepalived_config(value, recurse) -}}
{%- for item in data|dictsort -%}
{{- item[key] }} {{ keepalived_config(item[value], recurse) -}}
{%- endfor -%}
{%- if recurse > 0 -%}
{{- '}\n' -}}
{%- endif -%}
{%- else -%}
{{- '{\n' -}}
{%- for value in data -%}
{{- keepalived_config(value) -}}
{%- for item in data -%}
{{- keepalived_config(item) -}}
{%- endfor -%}
{{- '}\n' -}}
{%- endif -%}

View File

@ -49,5 +49,10 @@ class TestKeepalivedConfiguration(unittest.TestCase):
result = 'friends {\nrubble barney\n}\n'
self.renderTest(testdata, result)
def test_key_ordered_hashs(self):
testdata = {'friends': {'fred': 'flintstone', 'barney': 'rubble', 'wilma': 'flintstone', 'betty': 'rubble'}}
result = 'friends {\nbarney rubble\nbetty rubble\nfred flintstone\nwilma flintstone\n}\n'
self.renderTest(testdata, result)
if __name__ == '__main__':
unittest.main()