Using Jinja2 loops and conditions

Loops and conditions in Jinja2 are used to enhance our template and add more functionality to it. We will start by understanding how to add the for loop inside the template in order to iterate over passed values from YAML. For example, we may need to add a switch configuration under each interface, such as using the switchport mode and configure the VLAN ID which will be configured under the access port, or configure the allowed VLANs range in the case of the trunk ports.

On the other hand, we may need to enable some interfaces in the router and add custom configurations to it, such as MTU, speed, and duplex. So, we will use the for loop.

Notice that part of our script logic will now be moved from Python ...

Get Hands-On Enterprise Automation with Python. now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.