Last active
February 20, 2019 15:47
-
-
Save e3prom/a35df51b6154d4862dd389e8a58a771b to your computer and use it in GitHub Desktop.
Simple automation script to configure OSPF from YANG data model, on IOS-XE using RESTCONF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Automation skeleton for IOS-XE (RESCONF) with YANG | |
# Nicolas Chabbey <e3prom> | |
# | |
# conf/devices.conf: | |
# devices: | |
# - hostname: 10.240.0.180 | |
# type: cisco-ios-xe | |
# port: 443 | |
# verify: False | |
# api_root: /restconf | |
# username: ducky | |
# password: youbetcha | |
# | |
# data/router/ospf.yml: | |
# Cisco-IOS-XE-ospf:ospf: | |
# - id: 10 | |
# router-id: 10.240.0.180 | |
# network: | |
# - ip: 10.240.0.180 | |
# mask: 0.0.0.0 | |
# area: 0 | |
# | |
import json | |
import requests | |
import yaml | |
import argparse | |
import pdb | |
from requests.auth import HTTPBasicAuth | |
def config_load(): | |
f_devices = open("conf/devices.conf", "r") | |
devices = yaml.safe_load(f_devices) | |
config = devices #To update | |
return config | |
def get_ospf(device): | |
respath = '/data/Cisco-IOS-XE-native:native/router' | |
url = 'https://' + device['hostname'] + ':' + str(device['port']) + device['api_root'] + respath | |
auth = HTTPBasicAuth(device['username'], device['password']) | |
headers = device['headers'] | |
resp = requests.get(url, auth=auth, headers=headers, verify=device['verify']) | |
response = json.loads(resp.text) | |
print(json.dumps(response, indent=4)) | |
def set_ospf(device): | |
ospf_data = yaml.load(open('data/router/ospf.yml').read()) | |
ospf_obj = { | |
"Cisco-IOS-XE-native:router": ospf_data | |
} | |
respath = '/data/Cisco-IOS-XE-native:native/router' | |
url = 'https://' + device['hostname'] + ':' + str(device['port']) + device['api_root'] + respath | |
auth = HTTPBasicAuth(device['username'], device['password']) | |
headers = device['headers'] | |
# PATCH method is adding objects, PUT is overwriting | |
# everything recursively under the specified resource. | |
resp = requests.patch(url, auth=auth, headers=headers, data=json.dumps(ospf_obj), verify=device['verify']) | |
print(resp.status_code) | |
def main(): | |
config = config_load() | |
requests.packages.urllib3.disable_warnings() | |
headers = { | |
'Accept': 'application/yang-data+json', | |
'Content-Type': 'application/yang-data+json' | |
} | |
for device in config['devices']: | |
device['headers'] = headers | |
set_ospf(device) | |
get_ospf(device) | |
#pdb.set_trace() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment