diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index f46e018..832678d 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,17 +1,24 @@ -{%- macro keepalived_config(data) -%} +{%- macro keepalived_config(data, recurse=-1) -%} + {%- set recurse = recurse + 1 -%} {%- if data is none -%} -{{ '\n' }} - {%- elif data is mapping -%} - {%- for key, value in data.iteritems() -%} -{{ key }} {{ keepalived_config(value) }} - {%- endfor -%} + {{- '\n' -}} {%- elif data is string or data is number -%} -{{ data }}{{ '\n' }} - {%- else -%} -{{ '{\n' }} - {%- for value in data -%} -{{ keepalived_config(value) }} + {{- data }}{{ '\n' -}} + {%- elif data is mapping -%} + {%- if recurse > 0 -%} + {{- '{\n' -}} + {%- endif -%} + {%- for key, value in data.iteritems() -%} + {{- key }} {{ keepalived_config(value, recurse) -}} {%- endfor -%} -{{ '}\n' }} + {%- if recurse > 0 -%} + {{- '}\n' -}} + {%- endif -%} + {%- else -%} + {{- '{\n' -}} + {%- for value in data -%} + {{- keepalived_config(value) -}} + {%- endfor -%} + {{- '}\n' -}} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index d8add52..55b46a1 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -44,5 +44,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'flintstone {\nfred\nwilma\npebbles\n}\n' self.renderTest(testdata, result) + def test_key_hash_pair(self): + testdata = {'friends': {'rubble': 'barney'}} + result = 'friends {\nrubble barney\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main()