Added key hash pairs.

This commit is contained in:
Jeff Baskin 2017-01-18 20:40:19 -05:00
parent d66a776ddb
commit f96a5b9b4b
2 changed files with 24 additions and 12 deletions

View File

@ -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 -%}

View File

@ -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()