Chapter 8 Data Models and Dynamic Templates

For diagrams, tables, or exact code formatting, .

JOHN W. CAPOBIANCOPRINTED PAGE 137
logging trap debugging 
line con 0 
 session-timeout 5  
exec-timeout 20 0 
 transport output none 
 stopbits 1 
line vty 0 4 
 session-timeout 15  
access-class 10 in 
 exec-timeout 20 0 
 privilege level 15 
 transport input ssh 
 transport output none 
 stopbits 1 
line vty 5 15 
 session-timeout 15  
access-class 10 in 
 exec-timeout 20 0 
 privilege level 15 
 transport input ssh 
 transport output none 
 stopbits 1

Basic Programmatic Logic

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 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 %} 
 
{%  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