{% macro fluentd_option_block(name, values, padding = 2) -%}
  {% if values is mapping -%}
    {% set postfix = '' if values.__postfix is not defined else (' ' + values.__postfix) -%}

    <{{ name ~ postfix}}>
      {% for option in values | dict2items -%}
        {% if option.key != '__postfix' -%}
          {{ '' if padding == 0 else (' ' * padding) -}}
          {% if option.value is mapping -%}
            {{ fluentd_option_block(option.key, option.value, padding + 2) -}}
          {% else -%}
            {% if option.key[0] == '_' -%}
              {{ '@' ~ option.key[1:] }} {{ option.value }}
            {% else -%}
              {{ option.key }} {{ option.value }}
            {% endif -%}
          {% endif -%}
        {% endif -%}
      {% endfor -%}
    {{ '' if padding == 0 else (' ' * (padding - 2)) -}}</{{ name }}>
  {% endif -%}
{% endmacro -%}



{{ fluentd_option_block('system', fluentd_system | d(None)) }}


{% for source in (fluentd_sources | d([])) + (groups['all'] | map('extract', hostvars) | selectattr('fluentd_source', 'mapping') | map(attribute='fluentd_source') | list) -%}
  {{ fluentd_option_block('source', source) }}
{% endfor -%}


{% for filter in (fluentd_filters | d([])) -%}
  {{ fluentd_option_block('filter', filter) }}
{% endfor -%}


{% for match in (fluentd_matches | d([])) -%}
  {{ fluentd_option_block('match', match) }}
{% endfor -%}
