Make Template-based Configuration Changes
You can make template-based configuration changes using Python. The
jnpr.junos.utils.config module provides a
Config.load()
method that enables
Jinja2-based template changes. You indicate the template file and a Python dictionary of variable name/values.
2 |
host-name {{ host_name }}; |
3 |
domain-name {{ domain_name }}; |
Load this template change using the following Python by importing the Config utility widget and binding it to a Device variable:
1 |
>>> from jnpr.junos.utils.config import Config |
Define your template variables in a Python dictionary.
IMPORTANT: It is critical that the dictionary keys (names) match the variables defined in the Jinj2a template (the names inside the double-braces).
2 |
>>> myvars[ 'host_name' ] = 'jeremy' |
3 |
>>> myvars[ 'domain_name' ] = 'homelab.net' |
Finally, load the changes onto the device using the Config.load()
method:
1 |
>>> cu.load(template_path = 'system.conf' , template_vars = myvars) |
You can check the difference between the running configuration and the candidate configuration using the Config.pdiff()
method:
5 |
+ domain-name homelab.net; |
The Config.load()
method is a very versatile method for loading both static and template based content. The content can come from Python variables or files. For documentation on the Config.load()
method, enter help(Config.load)
at the Python shell.
Of particular note is how Config.load()
determines the format of the change. You can make changes to the Junos OS configuration using one of three formats:
The set format was added to Junos OS 11.3 Release or later. You can override the auto-detect of format by using the format
option.
The Config.load()
only loads the changes into the candidate configuration, but does not commit the changes. To commit the changes, you use the Config.commit()
method.