There are many ways to use and implement automation, This article will provide an example of automating configurations for network devices with standard config templates by given user requirements using python. Python is most user friendly language to learn and use for infrastructure engineers or any one. I want to build a web application that user can enter details in the form and submit, app will automate process of building configuration and display it on web page as result. I need a web framework to build web application which will run and perform front-end operations. Luckily python does has a light weight framework called Flask, It does lot many things in more simple and efficient way and mostly simple to code and build. So, let’s get started!

We have three parts in this project:

    1. User Interface
    2. Template
    3. controller

        User Interface is where user provide input data, Template is a standard dynamic file that will be updated based on variable values given and Controller will perform all operations for example, over here it will get the input variable values from html form and feed the template and then post the updated template data on to the html page.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Client Form</title>
</head>
<body>
    <h1>Client Form</h1>
    <form method="POST">
        <label for="name ">Name:</label>
        <input type="text" name="name"><br><br>
        <label for="id ">ID:</label>
        <input type="text" name="id"><br><br>
        <button>Submit</button>
    </form>
</body>
</html>

A jinja template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template. file format could be an extension with “jinja” or “j2“.
Jinja template file:

hostname {{ name }}

interface Loopback0
ip add 0.0.0.{{ id }} 255.255.255.255

Main python flask application(controller) file:

from flask import Flask
from flask import render_template, request
from jinja2 import Environment, FileSystemLoader

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/', methods=['GET', 'POST'])
def main():
    if request.method == 'POST':
        input1 = request.form
        env = Environment(loader=FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True,)
        template = env.get_template('tempinterface.j2')
        result = template.render(input1)
        return render_template('result.html', result=result)


if __name__ == '__main__':
    app.run(debug=True)

Now rendered result from jinja template should be displayed on web page. So, flask will render composed result on web page through variable ”result“. Below will be Result html file which will display the composed configuration based on user input from jinja2 template standard template on web browser and you can say html file is in jinja format when you see {{ }} and variable is defined in it.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Result!</title>
</head>
<body>
<pre>
{{ result }}
    </pre>
</body>
</html>

Result:

Note: For complete project code refer to Github repositor.

Login

Welcome to Typer

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join Typer
Registration is closed.