Basic Programmatic Logic
Let's learn some basic programming concepts like "if" statements and "for" loops using the Jinja2 templates.
We'll cover the following...
Basics
Even a beginner can write the basic logic statements required to create dynamic templates. Having
already created the data dictionary, it is now a simple matter of learning and understanding the syntax
required to unlock the information. The previous example, 01_ios_global.j2
, also contains some dynamic
templating required to handle the various unique platform differences or to substitute dynamic
information to variables. Here is the rest of the template that includes the logical statements. Note that there
are no spaces in the real template; they have been added to help with readability:
hostname {{ host_defaults.hostname }}{% if platform_defaults.type == 6000 %}boot system bootdisk:{{ platform_defaults.boot }}{% endif %}{% if platform_defaults.type == 4000 %}boot system bootflash:{{ host_defaults.boot }}{% endif %}logging buffered 64000{% if host_defaults.logging_levels is defined %}{% for logging_level in host_defaults.logging_levels %}{% for logging_target in host_defaults.logging_levels[logging_level] %}logging {{ logging_target }} {{ logging_level }}{% endfor %}{% endfor %}{% endif %}{% if platform_defaults.type == 6000 or platform_defaults.type == 4000 %}service compress-configno ip bootp serverdiagnostic bootup level complete{% endif %}errdisable recovery interval 60{% if platform_defaults.type == 6000 or platform_defaults.type == 4000 or platform_defaults.type == 3850 %}redundancymode sso{% endif %}enable secret 5 {{ global_campus_defaults.enable_secret }}username admin privilege 15 secret 5 {{ global_campus_defaults.enable_secret }}aaa new-model{% if platform_defaults.type == 6000 %}platform ip cef load-sharing ip-onlyplatform sub-interface maximum-vlan vlan-id 2048platform sub-interface maximum-vlan enableplatform rate-limit layer2 port-security pkt 300 burst 10no logging event link-status bootlogging event link-status default{% endif %}{% if platform_defaults.type == 3750 or platform_defaults.type == 3850 %}stack-mac persistent timer 0{% endif %}clock timezone est -5 0clock summer-time EDT recurringip arp proxy disableudld enableno ip domain-lookupip domain-name {{ global_campus_defaults.domain }}vtp domain NULLvtp mode transparent{% if platform_defaults.type == 4000 %}power redundancy-mode redundant{% endif %}login on-failure loglogin on-success logspanning-tree mode rapid-pvstspanning-tree extend system-id{% if host_spanning_tree_values is defined %}{% for host_spanning_tree_value in host_spanning_tree_values %}{% for vlan_list in host_spanning_tree_values[host_spanning_tree_value] %}spanning-tree vlan {{ vlan_list }} priority {{ host_spanning_tree_value }}{% endfor %}{% endfor %}{% endif %}{% if platform_defaults.type == 3750 %}port-channel load-balance src-dst-ip{% else %}{% if platform_defaults.type == 6000 %}port-channel load-balance src-dst-mixed-ip-port{% else %}{% if platform_defaults.type == 4000 or platform_defaults.type == 3850 %}port-channel load-balance src-dst-port{% endif %}{% endif %}{% endif %}{% if platform_defaults.type != 3850 %}vlan internal allocation policy ascending{% endif %}ip ssh version 2{% if platform_defaults.type == 3750 or platform_defaults.type == 2960 %}{% else %}no ip forward-protocol nd{% endif %}no ip http serverno ip http secure-serverlogging trap debugging{% for logging_host in global_campus_defaults.logging_hosts %}logging host {{ logging_host }}{% endfor %}
The template is composed of a mix of static explicit data replacement, if
/else
/end if
logic, and for
loop logic.
Every device requires a hostname. This is a good place to start looking closer at the format of the template.
...