Junos OS commit scripts enforce custom configuration rules and can automatically change the
configuration when it does not comply with your custom configuration rules. To generate
a persistent change or transient change using Python commit
scripts:
- At the start of the script, include the Python boilerplate
from Required Boilerplate for Commit Scripts, which is reproduced here for convenience:
from junos import Junos_Configuration
import jcs
if __name__ == '__main__':
# insert your code here
- Include one or more programming instructions that test
for your custom configuration rules.
For example, the following code selects each SONET/SDH
interface that does not have the MPLS protocol family enabled:
# Get configuration root object
root = Junos_Configuration
for element in root.xpath("./interfaces/ \
interface[starts-with(name,'so-')]"):
if element.find('unit/family/mpls') is None:
- Create an XML string that instructs Junos OS how to modify
the configuration.
This example enables the MPLS protocol family for the selected
interfaces.
if_name = element.find('name').text
unit_name = element.find('unit/name').text
change_xml = """
<interfaces>
<interface>
<name>{0}</name>
<unit>
<name>{1}</name>
<family>
<mpls>
</mpls>
</family>
</unit>
</interface>
</interfaces>
""".format(if_name, unit_name).strip()
- To generate the persistent or transient change, call the
jcs.emit_change
method, and specify the type of change,
either 'change' or 'transient-change', in the argument list. jcs.emit_change(change_xml, "change", "xml")
-
Include any additional required or optional code. This example generates a warning
message that is displayed on the CLI when the commit script updates a SONET/SDH
interface.
jcs.emit_warning("Adding 'family mpls' to SONET interface: " + if_name)
- Save the script with a meaningful name.
- Copy the script to either the /var/db/scripts/commit directory on the device hard disk or the /config/scripts/commit directory on the flash drive.
Note: Unsigned Python scripts must be owned by either root or
a user in the Junos OS super-user
login class, and only
the file owner can have write permission for the file.
- Enable the script by configuring the
file filename
statement at the [edit system scripts
commit]
hierarchy level.[edit]
user@host# set system scripts commit file filename
-
If the script generates transient changes, configure the
allow-transients
statement.
Configure the statement at the [edit system scripts commit]
hierarchy level to enable all commit scripts to make transient changes.
[edit system scripts]
user@host# set commit allow-transients
Alternatively, on supported devices and releases, configure the statement at the
[edit system scripts commit file filename]
hierarchy level to enable only the individual script to make transient
changes.
[edit system scripts]
user@host# set commit file filename allow-transients
- Enable the execution of unsigned Python scripts on the
device.
[edit system scripts]
user@host# set language (python | python3)
-
Commit the configuration.
[edit system scripts]
user@host# commit
The resulting script searches for SONET/SDH interfaces that
do not have the MPLS protocol family enabled, adds the family
mpls
statement at the [edit interfaces so-fpc/pic/port unit logical-unit-number]
hierarchy level as a persistent
change, and emits a warning message stating that the configuration
has been changed.
from junos import Junos_Configuration
import jcs
def main():
# Get configuration root object
root = Junos_Configuration
for element in root.xpath("./interfaces/ \
interface[starts-with(name,'so-')]"):
if element.find('unit/family/mpls') is None:
if_name = element.find('name').text
unit_name = element.find('unit/name').text
change_xml = """
<interfaces>
<interface>
<name>{0}</name>
<unit>
<name>{1}</name>
<family>
<mpls>
</mpls>
</family>
</unit>
</interface>
</interfaces>
""".format(if_name, unit_name).strip()
jcs.emit_change(change_xml, "change", "xml")
jcs.emit_warning("Adding 'family mpls' to SONET interface: " + if_name)
if __name__ == '__main__':
main()
If all enabled commit scripts run without errors, any persistent changes are loaded into the
candidate configuration. Any transient changes are loaded into the
checkout configuration, but not to the candidate configuration. The commit process then
continues by validating the configuration and propagating changes to the affected
processes on the device.
To display the configuration with both persistent and
transient changes applied, issue the show | display commit-scripts
configuration mode command.
[edit]
user@host# show | display commit-scripts
To display the configuration with only persistent changes
applied, issue the show | display commit-scripts no-transients
configuration mode command.
[edit]
user@host# show | display commit-scripts no-transients
Persistent and transient changes are loaded into the configuration
in the same manner that the load replace
configuration
mode command loads an incoming configuration. When generating a persistent
or transient change, adding the replace="replace"
attribute to a configuration element produces the same behavior
as a replace:
tag in a load replace
operation. Both persistent and transient changes are loaded into
the configuration with the load replace
behavior. However,
persistent changes are loaded into the candidate configuration, and
transient changes are loaded into the checkout configuration.