3.2. Custom Modifications

Apart from usual previous steps:
  • We can now add the custom classes, modules to be accessible over jinja templates.

  • We can now override device var databse with custom (regional_file) database.

Refer below details on how to use them.

  • Full module import should declare pure methods only.

  • Classes should be imported explicitely from diverse modules.


Detailed How To

  1. Import necessary custom class(es) from module, and/or import necessary module(s).

    Below depicts a Sample Code. Modify it as per your custom class(es) requirements.

    # Imports
    from custom_j2config.classes import Summaries, Vrf, Vlan, Bgp, Physical
    from custom_j2config import module1
    from custom_j2config.regional import Region    # available for j2config verion > 0.0.6
    
    # Custom global/Regional static variable excel database to override device local variables (optional)
    regional_file = 'global.xlsx'                # point to custom file
    
  2. Include all imported classes and modules in to a dictionary and set respectively as given sample below.

    custom_classes = {           ### add all impored classes here ###
            'Summaries': Summaries,
            'Vrf': Vrf,
            'Vlan': Vlan,
            'Bgp': Bgp,
            'Physical': Physical
    }
    custom_modules = {module1, }  ### add all imported modules here ###
    
  3. Modify PrepareConfig instance filters (created in previous page)

    ### From previous step ###
    PrCfg = PrepareConfig(
            data_file=data_file,
            jtemplate_file=template_file,
            output_folder=output_path,             ## optional (default: ".")
            regional_file=regional_file,           ## default: None
            regional_class=Region                  ## default: None
    )
    
    ### Add below two additional steps to include custom class/module methods as filter to jinja processsing.
    ### 1. Add Custom classes to above instance using `custom_class_add_to_filter`.
    PrCfg.custom_class_add_to_filter(**custom_classes)
    
    ### 2. Add Custom modules to above instance using `custom_module_methods_add_to_filter`.
    PrCfg.custom_module_methods_add_to_filter(*custom_modules)
    
  4. Start configuration generation

    PrCfg.start()
    

Note

Congratulations!!!

  1. Hurrey!!! Now you can access custom declared classes/methods from within jinja template.

  2. And you can override device var database using custom regional_file database.

Notice

  • Make a note that output generates based on jinja template and template variables.

  • It is soleley users responsiblity for providing appropriate filters as custom_classes and custom_modules, as well as using those in jinja templates.

  • Make sure to cross-check the generated facts before using it.