{% include "head.html" %}

{% macro display_result(result, hdr) %}
    {% if result is mapping -%}
        {% if 'item' in result -%}
            <{{ hdr }}>Item</{{ hdr }}>
            {{ result.item | pygments_formatter | safe }}
        {% endif %}

        {% for attr in result.keys() | sort if attr not in ['item', 'changed', 'stdout_lines'] -%}
            {% if result[attr] | default(False) -%}
                <{{ hdr }}>{{ attr | title }}</{{ hdr }}>
                {{ result[attr] | pygments_formatter | safe }}
            {% endif -%}
        {% endfor -%}
    {% elif result is iterable and result is not string %}
        {% for item in result -%}
            {% set item = item | from_json %}
            <div class="panel panel-default">
                <div class="panel-body">
                    {{ display_result(item, 'h2') }}
                </div>
            </div>
        {% endfor %}
    {% else -%}
        {{ result | pygments_formatter | safe }}
    {% endif -%}
{% endmacro %}

<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12 results">
          <h2>Task: <strong>{{ task_result.task.name }}</strong></h2>
          {% if task_result.task.tags and task_result.task.tags | from_json | length > 0 -%}
          <h2>Tags: <strong>{{ task_result.task.tags | from_json | join(', ') }}</strong></h2>
          {% endif -%}
          <h2>Host: <strong>{{ task_result.host.name }}</strong></h2>
          <p>Time: <strong>{{ task_result.time_start | datefmt }}</strong></p>
          <p>Ansible version: <strong>{{ task_result.task.playbook.ansible_version }}</strong></p>
          {% set result = task_result.result | from_json %}
          {% if 'results' in result %}
              {% for item in result.results %}
                  <h2>Result #{{ loop.index }}</h2>
                  {{ display_result(item, 'h3') }}
              {% endfor %}
          {% else %}
              {{ display_result(result, 'h2') }}
          {% endif %}
        </div>
    </div>
</div>
</body>
</html>
