Topic: tech juniper ijaut prev next
tech juniper ijaut > 08: Junos PyEZ Configuration
The ‘jnpr.junos.utils.config.Config’ utility can be used to retrieve, load, roll back and compare the device configuration. Use ASCII text files, XML elements, Junos ‘set’ commands, or JSON to deploy configuration.
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
with Device(host='172.25.11.1') as dev:
with Config(dev, mode="exclusive") as cu:
cu.load(url="/var/home/lab/ijaut/example.conf", overwrite=True)
cu.commit()
print("Device configuration loaded successfully")
In networks that use automation extensively, it is recommended that you use a private configuration database, becuase locking the shared configuration database can afect other automation tools.
from jnpr.junos import Device
from lxml imoprt etree
with Device(host='172.25.11.1') as dev:
cnf = dev.rpc_get_config(filter_xml=etree.XML('<configuration><interfac$
print(etree.tostring(cnf, encoding='unicode', pretty_print='true'))
The Config utility provides ‘pdiff()’ and ‘diff()’ methods to provide the difference between the active configuration and the rollback configuration, as standard output and an object, respectively. This is equivalent to the command;
show | compare rollback
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
dev = Device(host='172.25.11.1').open()
with Config(dev, mode='exclusive') as cu:
# Load a candidate configuration on the device
cu.load(url='/var/home/lab/ijaut/compare.conf', overwrite=True)
# Print differences to the standard output
cu.pdiff()
# Roll back to the active configuration
cu.rollback(rb_id=0)
dev.close()
By default, the ‘pdiff()’ method generates the difference between the candidate and active configurations. Use the ‘rb_id’ option to ‘pdiff()’ to perform the comparison between the candidate configuration and a rollback configuration. The output of ‘pdiff()’ is in human readable Junos ‘patch’ format.
with Device(host='172.25.11.1') as dev:
with Config(dev, mode='exclusive') as cu:
cu.rescue(action="save")
with Device(host='172.25.11.1') as dev:
with Config(dev, mode='exclusive') as cu:
rescue = cu.rescue(action="reload")
if rescue is False:
print("No rescue configuration exists")
else:
cu.commit()
print("Rescue configuration successfully loaded")
Use ASCII text files, XML elements, Junos ‘set’ commands, or JSON to deploy configuration. The configuration file can be local, or a URL reachable from the device.
With the ‘Config.load()’ method, the ‘url’ option is used to load a file from the administrator’s workstation. The ‘path’ option is used to load a file from the Junos device.
The configuration changes can be passed to ‘Config.load()’ as a string.
conf.load(data, format="text")
The configuration changes can be checked before the commit is attempted.
conf.lock()
conf.load(data, format="text")
if conf.commit_check():
conf.commit()
else
conf.rollback()
conf.unlock()
The configuration changes can be loaded as ‘set’ commands instead.
conf.load(data, format="set")
Check if the changes to be committed already exist.
diff = conf.diff()
if diff is None:
print("Configuration already up to date")
else:
conf.commit()
Set the confirmation timeout to five minutes. This is equivalent to a Junos CLI ‘commit confirm’ command.
cu.commit(confirm=5)
Use the ‘commit_check()’ method to confirm the configuration change.
cu.commit_check()
print("Device configuration checked successfully")
This can be tested by SSH’ing to the device. The ‘commit confirm’ timer will be displayed on login.
A commit comment can be added with the ‘comment’ argument to the ‘commit()’ method.
On Junos devices with dual routing engines, the ‘sync=True’ argument to ‘commit()’ will synchronise the configuration to both routing engines. This is equivalent to a ‘commit synchronise’ command. An exception is generated if ‘sync=True’ is applied to single routing engine platforms, but the commit operation can still succeed.
The ‘jnpr.junos.exception’ module provides Junos specific exceptions.
Exceptions in Python are implemented using try/except statements.
try:
dev = Device(host="172.25.11.1")
dev.open
except ConnectError:
print("Could not connect to device")
finally:
# Operations to be executed even if exceptions are generated
dev.close()
Other exceptions are LockError, ConfigLoadError and CommitError.
Exceptions can be imported with;
from jnpr.junos.exception import *
or by importing indivudal exceptions.
PyEZ uses Jinja2 and YAML variable files. In the template file, the configuration values are replaced by Jinja2 variables (for instance, ‘{{bgp.interface}}’).
---
bgp:
interface: ge-0/0/0
unit_num: 0
loopback:
unit_num: 0
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
import yaml
with open("vmx-01.yml", 'r') as fh:
data = yaml.safe_load(fh)
with Device(host="172.25.11.1") as dev:
with Config(dev, mode="exclusive") as cu:
conf.load(
template_path="example.j2",
template_vars=data,
format="text"
)
conf.commit()