From 80645badee7c108656c108a76287c2ae23d3268c Mon Sep 17 00:00:00 2001 From: Gilles Dartiguelongue Date: Fri, 8 Apr 2016 18:27:15 +0200 Subject: [PATCH] Handle mappings passed as dict or OrderedDict --- postfix/files/mapping.j2 | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/postfix/files/mapping.j2 b/postfix/files/mapping.j2 index e403ac3..5045b1f 100644 --- a/postfix/files/mapping.j2 +++ b/postfix/files/mapping.j2 @@ -1,19 +1,23 @@ # Managed by config management -{% if data is mapping -%} - {% for key, value in data.iteritems() -%} - {# Some settings, like virtual_alias_maps can take multiple values. Handle this case. -#} - {% if value is iterable and value is not string -%} - {% for item in value -%} +{%- macro format_value(key, value) %} + {#- Some settings, like virtual_alias_maps can take multiple values. Handle this case. -#} + {%- if value is iterable and value is not string -%} + {%- for item in value %} {{ key }} {{ item }} - {% endfor -%} - {% else -%} + {%- endfor %} + {%- else -%} {{ key }} {{ value }} - {% endif -%} - {% endfor -%} -{% else -%} - {# Some settings need order, handle OrderedDict -#} - {% for item in data -%} -{{ item.keys()[0] }} {{ item.values()[0] }} -{% endfor -%} -{% endif -%} + {%- endif -%} +{%- endmacro %} + +{%- if data is mapping %} +{% for key, value in data.iteritems() %} +{{ format_value(key, value) }} +{%- endfor -%} +{%- else %} +{#- Some settings need order, handle OrderedDict #} +{% for item in data %} +{{ format_value(item.keys()[0], item.values()[0]) }} +{%- endfor -%} +{%- endif %}