Guidelines for Configuration Template

#Substitution, Conditioning, and Looping in the Template should be in the Jinja2 standard. For more tutorials, refer to http://jinja.pocoo.org/docs/2.10/.

Points to Remember

Always enclose the commands within {% %} for "if," and "for," and "while" conditional statements

Always enclose the variables inside {{ }} for substitution

Sample Template Configuration

Example 1: Create an Empty List, add values to the List, and DO a simple ‘For Loop’

# Declaring a string variable to store value from Runtime or user. The default (“”) function will make the variable empty string till the USER input

{% set myinput = Runtime.interface_list | default(“”) %}

# Converting User Input to a list using the Split function

{% set mylist = myinput .split(",") %}

#Doing for Loop or Looping of Each Item

{% for each_interface in mylist %}
      <command prompt="#">int {{each_interface}}</command>
      <command prompt="#">shutdown</command>

# for requires endfor to close the section

{% endfor %}

Example 2: Conditions (if case elif Case and else case)

{% for each_interface in mylist %}
        {% if each_interface == "Gi0/1" %}
                  <command prompt="#">int {{each_interface}}</command>
                  <command prompt="#">shutdown</command>
        {% elif each_interface == "Gi0/2" %}
                   <command prompt="#">int {{each_interface}}</command>
                   <command prompt="#">no shutdown</command>
        {% else %}
                    <command prompt="#">I dont know</command>
        {% endif %}
{% endfor %}

Example 3: Taking List Input from a Trigger

#down_interface_list_cisco_ios is a Trigger in Configuration Trigger

{% set mylist1 = Trigger.down_interface_list_cisco_ios | default([]) %}
{% for each_interface in mylist1 %}
      <command prompt="#">int {{each_interface}}</command>
      <command prompt="#">shutdown</command>
{% endfor %}

Example 4: Disable the Interface named ‘Ether’

# String Manipulation startswith, endswith, find, lower, upper, strip

{% set myinput = Runtime.InterfaceNames | default ("") %}
{% set mylist = myinput.split(",") %}
{% for each_item in mylist %}
      {% if each_item.lower().startswith("ether") %}        
            <command prompt="#">int {{each_item}}</command>
            <command prompt="#">shutdown</command>
   {% endif %}
{% endfor %}

Example 5: Taking the First Element from the Trigger

{% set mylist = Trigger.down_interface_list_cisco_ios |default ([]) %}\

{% if mylist %}
    <command timeout ="10" prompt ="#" >config t</command>        
    <command timeout ="10" prompt ="#" >interface {{mylist[0]}} </command>
    <command timeout="10" prompt  ="#" >IP address 172.17.230.2    
255.255.255.252</command>
     <command timeout ="10" prompt  ="#" >no shut</command>
{% endif %}

Last updated