Real-time Capture and Display of Ansible Output Logs in HTML using Flask and Python

What You Will Learn

Explore how to capture real-time Ansible output logs and present them in HTML using Flask and Python.

Introduction to the Problem and Solution

When executing Ansible tasks, monitoring output logs in real-time is essential for debugging and tracking progress. By combining Flask with Python, we can develop a web application that dynamically captures these logs and presents them visually in HTML. This solution simplifies the monitoring process during Ansible operations, providing a convenient way to stay informed about task execution.


# Import necessary libraries
from flask import Flask, Response
import subprocess

app = Flask(__name__)

def index():
    def run_ansible_command():
        proc = subprocess.Popen('ansible-playbook your_playbook.yml', shell=True, stdout=subprocess.PIPE)

        while True:
            # Read command standard output line by line
            line = proc.stdout.readline().decode()

            if not line:

            # Yield each line to the client for real-time display using server-sent events (SSE)
            yield f"data: {line}<br>\n"

    return Response(run_ansible_command(), content_type='text/event-stream')

if __name__ == '__main__':

# Copyright PHD

(Code snippet provided by

Remember to replace ‘your_playbook.yml’ with the path to your actual playbook file.


To achieve real-time capture and display of Ansible output logs in HTML via Flask and Python:

  1. Utilize the subprocess module to run an Ansible playbook command within the Flask route.
  2. Continuously read the standard output of the subprocess line by line in the run_ansible_command() function.
  3. Send each output line back to the client using Server-Sent Events (SSE) for live updates on the webpage.
  4. By running this Flask application alongside your Ansible tasks, you can visualize live logs conveniently through a web interface.
    How do I install Flask?

    Flask can be installed via pip by running pip install flask.

    Can I customize the HTML template for log visualization?

    Yes, you can modify the render_template_string section of the code to design a custom UI for displaying log messages.

    Is it possible to integrate this solution with other automation tools besides Ansible?

    While this tutorial focuses on Ansible, similar approaches can be applied for integrating with other automation frameworks like Puppet or Chef.

    How secure is it to expose real-time logs over HTTP?

    For production environments, consider implementing proper access control measures such as authentication mechanisms or serving content over HTTPS for enhanced security.

    What happens if my ansible-playbook command encounters errors?

    Error handling mechanisms should be implemented within run_ansible_command() function to gracefully handle exceptions or error messages from ansible-playbook executions.

    Can I enhance this setup further by incorporating database storage for logs?

    Yes, you can extend this project by saving log data into a database like SQLite or MySQL for persistent storage and historical analysis purposes.


    In conclusion: – Real-time monitoring of Ansible output logs through a web interface enhances visibility during automation tasks. – Integrating Flask with Python offers a simple approach towards creating interactive log displays effortlessly.

    Leave a Comment