Generate a Persistent or Transient Configuration Change in Python Commit Scripts
Junos OS commit scripts enforce custom configuration rules and can automatically change the configuration when it does not comply with your rules. When a commit script changes the configuration, it can generate a persistent change or a transient change.
To generate a persistent or transient change using Python commit scripts:
The resulting script searches for ge-0/0/* interfaces that do not have the MPLS protocol family
enabled. The script adds the family mpls statement at the [edit
interfaces ge-0/0/port unit
logical-unit-number] hierarchy level as a persistent
change. For each updated interface, the script emits a warning message stating that it
changed the interface configuration.
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,'ge-0/0')]"):
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 interface: " + if_name)
if __name__ == '__main__':
main()
If all enabled commit scripts run without errors, the device loads any persistent changes into the candidate configuration. The device loads any transient changes 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
The device loads persistent and transient changes 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.