Automation and Scripting
Automation and Scripting
The Automation and Scripting domain checks whether you can read, write, and reason about small automation programs and the data they exchange. Expect Python fundamentals, JSON/YAML/XML handling, Jinja2 templating, and Junos-specific automation with PyEZ, SLAX, and XSLT.
Core rules
- Junos PyEZ is a Python library that connects to a device over NETCONF (SSH, TCP 830 by default) and exposes operational RPCs and configuration objects.
- Use
Device(host=..., user=..., passwd=...),open(), andclose(); a context manager (with Device(...) as dev:) closes the session safely. - Configuration is loaded with
Config(dev);load()uses keyword semantics such asmerge=True,overwrite=True, orupdate=True.commit_check()validates without committing;commit()applies;rollback()reverts. - Jinja2 renders text from variables; YAML/JSON parse into Python dicts/lists. Do not hand-build XML with string concatenation when a template or library exists.
- Handle
RpcErrorandConnectErrorexplicitly; automation that ignores failures is not automation.
Worked example
Goal: set ge-0/0/1 description to uplink-to-core and verify syntax before committing.
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
with Device(host='10.0.0.1', user='auto', passwd='secret') as dev:
cu = Config(dev)
cu.load('set interfaces ge-0/0/1 description uplink-to-core', format='set', merge=True)
cu.commit_check()
cu.commit(comment='CI: set uplink description')
Reasoning: format='set' accepts Junos set commands, merge=True preserves existing config, commit_check() catches syntax or semantic errors, and commit() makes the change permanent. If commit_check() raises, the script stops before touching the live config.
Traps
- Assuming
commit_check()commits; it does not. - Using
overwrite=Truewhen you mean merge; overwrite replaces the entire candidate config. - Confusing NETCONF port 830 with SSH port 22.
- Parsing XML with regex instead of an XML parser.