...

/

Basic Programmatic Logic

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:

Press + to interact
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-config
no ip bootp server
diagnostic bootup level complete
{% endif %}
errdisable recovery interval 60
{% if platform_defaults.type == 6000 or platform_defaults.type == 4000 or platform_defaults.type == 3850 %}
redundancy
mode 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-only
platform sub-interface maximum-vlan vlan-id 2048
platform sub-interface maximum-vlan enable
platform rate-limit layer2 port-security pkt 300 burst 10
no logging event link-status boot
logging 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 0
clock summer-time EDT recurring
ip arp proxy disable
udld enable
no ip domain-lookup
ip domain-name {{ global_campus_defaults.domain }}
vtp domain NULL
vtp mode transparent
{% if platform_defaults.type == 4000 %}
power redundancy-mode redundant
{% endif %}
login on-failure log
login on-success log
spanning-tree mode rapid-pvst
spanning-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 server
no ip http secure-server
logging 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.

 ...