Skip to content

rickdonato/network-automation-101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Network Automation 101

Python example for generating configuration based on JSON or YAML input.

Configurator

The purpose of this sample project is to show the use of Python and Jinja2 to generate network devices configuration from JSON or YAML input.

The Python script will ingest a data variable file (can be JSON or YAML format) and will render a configuration file based on a template in the templates/ folder.

Requirements

  • Python 3.6
  • PyYAML: Python YAML parser.
  • Jinja2: Python template engine.

Note: This example will use a virtualenv. To learn more about virtual environments see: Python Virtual Environments a Primer

Initialization

# clone code
git clone git@bitbucket.org:packetflow/introduction-to-network-automation.git
cd introduction-to-network-automation

# install virtualenv
sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install virtualenv

# create virtualenv
virtualenv -p /usr/bin/python3 venv
source ./venv/bin/activate

# install deps in virtualenv
pip3 install -r ./requirements.txt

Example

Let's say we have the following variable file: data/interfaces_vars.json

{
    "interfaces": [
        {
            "name": "Vlan177",
            "address": "10.77.1.68 255.255.255.0",
            "description": "Lan In-Band Network",
            "load_interval": 5
        },
        {
            "name": "Management1",
            "description": "lab01 - Eth100/1/37",
            "enabled": true,
            "address": "10.17.17.177 255.255.255.0",
            "load_interval": 5
        }
    ]
}

The script uses sys.argv to get the arguments from the command line and use it as parameters to denote the variables file location and the template file location. It then renders the template and data and creates a text file with the Cisco-based interface configuration.

Now lets run the script:

python3.6 configurator.py data/interfaces_vars.json templates/cisco_interfaces.j2

This will create a Cisco configuration file with the interfaces information.

!
interface Vlan177
  description NO DESCRIPTION
  ip address 10.77.1.68 255.255.255.0
  load-interval 5
!
interface Management1
  description lab01 - Eth100/1/37
  ip address 10.17.17.177 255.255.255.0
  load-interval 5
  no shutdown
!
!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages