JSON Schema (for) Network as Code: Build JSON schemas from YAML
Project description
JSNAC
JSON Schema (for) Network as Code
Overview
The majority of Network and Infrastructure automation is done in YAML. Be it Ansible Host Variables, Network Device data to build a Jinja2 configuration with, or just a collection of data you want to run a script against to put into another product you have likely written a YAML file and have had to debug a typo or had to help another colleague build a new file for a new device.
In an ideal world you can (and should) put a lot of this data into a database or Network Source of Truth solution and pull from it so the validation is done for you. However, these solutions don't cover every use case so you will likely end up creating some YAML files here and there.
Using a JSON schema for validating & documenting your YAML is a good practice in a CI/CD world but is very cumbersome to create from scratch.
This project aims to simplify this whole process by helping you build a JSON schema using YAML syntax that has network and infrastructure templates (or sub-schemas) in mind.
Now you can hopefully catch those rare mistakes before you run that Playbook, create that configuration with a Jinja2 template or run a REST query to that Source of Truth or Telemetry solution :)
Brief Example
Take a basic Ansible host_vars YAML file for a host below:
chassis:
hostname: "ceos-spine1"
model: "ceos"
device_type: "router"
system:
domain_name: "example.com"
ntp_servers: [ "10.0.0.1", "10.0.0.2" ]
interfaces:
- if: "Loopback0"
desc: "Underlay Loopback"
ipv4: "10.0.0.101/32"
ipv6: "2001:2:a1::1/128"
- if: "Ethernet0"
desc: "Management Interface"
ipv4: "10.1.0.20/24"
You can simply write out how you would like to document & validate this data in YAML using kinds, and this program will write out a JSON schema you can use.
header:
title: "Ansible host vars"
schema:
chassis:
title: "Chassis"
type: "object"
properties:
hostname:
kind: { name: "string" }
model:
kind: { name: "string" }
device_type:
kind: { name: "choice", choices: [ "router", "switch", "firewall", "load-balancer" ] }
system:
type: "object"
properties:
domain_name:
kind: { name: "string" }
ntp_servers:
type: "array"
items:
kind: { name: "ipv4" }
interfaces:
type: "array"
items:
type: "object"
properties:
if:
kind: { name: "string" }
desc:
kind: { name: "string" }
ipv4:
kind: { name: "ipv4_cidr" }
ipv6:
kind: { name: "ipv6_cidr" }
We also have full support for writing your own titles, descriptions, kinds (sub-schemas), objects that are required, etc. A more fleshed out example of the same schema is below:
header:
id: "example-schema.json"
title: "Ansible host vars"
description: |
Ansible host vars for my networking device. Requires the below objects:
- chassis
- system
- interfaces
kinds:
hostname:
title: "Hostname"
description: "Hostname of the device"
type: "pattern"
regex: "^[a-zA-Z0-9-]{1,63}$"
schema:
chassis:
title: "Chassis"
description: |
Object containing Chassis information. Has the below properties:
hostname [required]: hostname
model [required]: string
device_type [required]: choice (router, switch, firewall, load-balancer)
type: "object"
properties:
hostname:
kind: { name: "hostname" }
model:
kind: { name: "string" }
device_type:
title: "Device Type"
description: |
Device Type options are:
router, switch, firewall, load-balancer
kind: { name: "choice", choices: [ "router", "switch", "firewall", "load-balancer" ] }
required: [ "hostname", "model", "device_type" ]
system:
title: "System"
description: |
Object containing System information. Has the below properties:
domain_name [required]: string
ntp_servers [required]: list of ipv4 addresses
type: "object"
properties:
domain_name:
kind: { name: "string" }
ntp_servers:
title: "NTP Servers"
description: "List of NTP servers"
type: "array"
items:
kind: { name: "ipv4" }
required: [ "domain_name", "ntp_servers" ]
interfaces:
title: "Device Interfaces"
description: |
List of device interfaces. Each interface has the below properties:
if [required]: string
desc: string
ipv4: ipv4_cidr
ipv6: ipv6_cidr
type: "array"
items:
type: "object"
properties:
if:
kind: { name: "string" }
desc:
kind: { name: "string" }
ipv4:
kind: { name: "ipv4_cidr" }
ipv6:
kind: { name: "ipv6_cidr" }
required: [ "if" ]
A full list of kinds are available in the
Usage
CLI
# Print the help message
jsnac -h
# Build a JSON schema from a YAML file (default file is jsnac.schema.json)
jsnac -f data/example-jsnac.yml
# Build a JSON schema from a YAML file and save it to a custom file
jsnac -f data/example-jsnac.yml -o my.schema.json
# Increase the verbosity of the output (this generates alot of messages as I use it for debugging)
jsnac -f data/example-jsnac.yml -v
Library
"""
This example demonstrates how to use the jsnac library to build a JSON schema from a YAML file in a Python script.
Example yml file is available here: <https://www.github.com/commitconfirmed/jsnac/blob/main/data/example-jsnac.yml>
"""
from jsnac.core.infer import SchemaInferer
def main():
# Create a SchemaInferer object
jsnac = SchemaInferer()
# Load the YAML data however you like into the SchemaInferer object
with open('data/example-jsnac.yml', 'r') as file:
data = file.read()
jsnac.add_yaml(data)
# Loading from JSON directly is also supported if needed
# jsnac.add_json(json_data)
# Build the JSON schema
schema = jsnac.build_schema()
print(schema)
if __name__ == '__main__':
main()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsnac-0.2.0.tar.gz.
File metadata
- Download URL: jsnac-0.2.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
020e3d8ccc4c3f814bfe81317af68404fe524147516ed6256cb32bbf9dae29be
|
|
| MD5 |
4f550dbe780f90e6b51be92bade41e9c
|
|
| BLAKE2b-256 |
7aa068e7c3acdf6455f0fd363886ea2370c5870526daa5f8a116a62440e3890f
|
File details
Details for the file jsnac-0.2.0-py3-none-any.whl.
File metadata
- Download URL: jsnac-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b280ea354fa383ab38b45a40fd7176fd5c71391515032fcad79363e76d6a368
|
|
| MD5 |
2a5d26b1393fe93f36aa8431d9aea083
|
|
| BLAKE2b-256 |
8f836c40a9ac35ae233fe72f09de9eae480e792f75ed542c2ee7201c8e0f650d
|