{% extends "layout.html" %}

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

    {% for attr in result.keys()|sort
      if attr not in ['item', 'changed', 'stdout_lines'] %}
      {% if result[attr]|default(False) %}
        <{{ hdr }}>{{ attr|title }}</{{ hdr }}>
        {% if result[attr] is string %}
          <pre>{{ result[attr] }}</pre>
        {% elif result[attr] is mapping or result[attr] is iterable%}
          <pre>{{ result[attr] |to_nice_json |safe }}</pre>
        {% else %}
          <pre>{{ result |to_nice_json |safe }}</pre>
        {% endif %}
      {% endif %}
    {% endfor %}
  {% else %}
    <pre>{{ result |to_nice_json |safe }}</pre>
  {% endif %}
{% endmacro %}

{% block content %}
<ol class="breadcrumb">
  <li>{{ macros.make_link('playbook.show_playbook', 'Playbook', playbook=task_result.task.playbook.id) }}</li>
  <li>{{ macros.make_link('play.show_play', 'Play', play=task_result.task.play.id) }}</li>
  <li>{{ macros.make_link('task.show_task', 'Task', task=task_result.task.id) }}</li>
  <li class="active"><strong>Host</strong>: {{ task_result.host.name }}</li>
</ol>

<h2>Task result information</h2>
<table class="summary table table-striped table-bordered table-hover table-condensed">
  <thead>
    <tr>
      <th>Start</th>
      <th>End</th>
      <th>Duration</th>
      <th>Action</th>
      <th>File</th>
      <th>Line</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>{{ task_result.time_start |datefmt }}</td>
      <td>{{ task_result.time_end |datefmt }}</td>
      <td>{{ task_result.duration |timefmt }}</td>
      <td>{{ task_result.task.action }}</td>
      <td>{{ task_result.task.path|pathtruncate }}</td>
      <td>{{ task_result.task.lineno }}</td>
    </tr>
  </tbody>
</table>

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

{% endblock %}
