{# Copyright (C) 2014-2020 Maciej Delmanowski <drybjed@gmail.com>
 # Copyright (C) 2014-2020 DebOps <https://debops.org/>
 # SPDX-License-Identifier: GPL-3.0-only
 #}
{% if postfix__env_persistent_maincf %}
{% set postfix__tpl_persistent_maincf = (
     lookup("file", secret + "/postfix/dependent_config/" + inventory_hostname + "/maincf.json",
            errors="ignore") | d("{}", True) | from_json) %}
{% else %}
{%   set postfix__tpl_persistent_maincf = {} %}
{% endif %}
{% if postfix__dependent_maincf %}
{%   for element in q("flattened", postfix__dependent_maincf) %}
{%     if element.role|d() and element.config|d() %}
{%       if element.state|d('present') not in [ 'ignore', 'absent' ] %}
{%         set _ = postfix__tpl_persistent_maincf.update( {element.role: q("flattened", element.config) }) %}
{%       elif element.state|d('present') == 'absent' %}
{%         set _ = postfix__tpl_persistent_maincf.pop(element.role, None) %}
{%       endif %}
{%     elif element is mapping and 'role' not in element.keys() and 'config' not in element.keys() %}
{%       for role, config in element.items() %}
{%         set _ = postfix__tpl_persistent_maincf.update( {role: q("flattened", config)} ) %}
{%       endfor %}
{%     endif %}
{%   endfor %}
{% endif %}
{{ postfix__tpl_persistent_maincf | to_nice_json }}
