diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 832678d..fa46d41 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -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 -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 55b46a1..1aed30a 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -12,7 +12,7 @@ class TestKeepalivedConfiguration(unittest.TestCase): 'keepalived', 'templates')) self.t_conf = Environment(loader=FileSystemLoader(self.t_dir), - trim_blocks=True) + trim_blocks=True) def renderTest(self, data, result): holder = self.t_conf.get_template('test_config.jinja').render(testdata=data) @@ -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()