How-To: Configuration management

By Elevate posted 08-05-2015 19:14




Configuration changes using unstructured snippets and templates, or using structured abstractions.




There are two ways to make configuration changes: unstructured and structured.  Unstructured changes are simply snippets of Junos OS configurations in any of the supported formats.  Templates are snippets that have variables, so that you can quickly and easily produce configurations.  Structured changes use well-defined Resource abstractions that have a programmatic approach to performing changes and accessing properties.  Resources are analogous to the structured abstractions you would find in IT frameworks such as Puppet and Chef.



Unstructured Changes


Unstructured changes are made using the Config utility. The Config utility provides a load() method that allows you to load either snippets or templates.


You associate a Config utility with a Device variable in one of two ways:


As a standalone variable:


1 >>> from jnpr.junos.utils.config import Config
2 >>>
3 >>> cu = Config(dev)
4 >>> cu
5 jnpr.junos.utils.Config(jnpr-dc-fw)


As a bound property:


1 >>> from jnpr.junos.utils.config import Config
2 >>>
3 >>> dev.bind(cu=Config)
4 >>>
5 jnpr.junos.utils.Config(jnpr-dc-fw)


Once you have associated the Config to the Device, you use it the same way.


Complete coverage of using the load() method is beyond the scope of this topic page.  But here are the highlights, and you can do "help(Config.load)" at the Python shell for complete documentation.


Loading a Snippet


You can load a snippet from a file very simply, for example:


1 >>> rsp = cu.load( path="config-example.conf" )


The load() method uses the filename extension by default to determine the configuration format ('text','set', or 'xml').  The result of theload() command is currently an XML structure that indicates the success or error of the load request. 

Note that load() does NOT commit the change, it only loads it.  You can use the Config utility commit_check() and commit()methods to perform those actions.


Loading a Template


Loading a template from a file is also very simple.  You provide a path to the template file and a dictionary of variables to be used by the template.  For example:


1 >>> tvars = dict(host_name='jeremy', domain_name='')
2 >>> rsp = cu.load( template_path="config-example-template.conf", template_vars=tvars )


Structured Changes


Structured changes use a Junos PyEZ widget called a Resource.  The Resource provides the necessary micro-framework to interact with the Junos OS configuration in a programmatic way.