Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python (Jinja2) Templates for Network Automation

4,124 views

Published on

High level overview of using Jinja2 Python Templates for Network Automation.

Published in: Technology

Python (Jinja2) Templates for Network Automation

  1. 1. PythonTemplates for Network Automation Rick Sherman VulgarPython.com
  2. 2. What are templates and why use them? A template is a text document where some or all of the content is automatically generated. Templates are text files in the format you already work with. Data is stored separated and automatically loaded. Easily re-used and shared.
  3. 3. Quick Example – Configuration file system { host-name tp8; name-server 192.168.5.68; domain-name shermdog.com; backup-router 192.168.71.254; root-authentication { encrypted-password "$1$hKWBk3fAgDHF/"; ## Secret data } } Below is an example of a Junos configuration. The configuration data is highlighted in blue.
  4. 4. Quick configuration template system { host-name {{ host_name }}; name-server {{ dns_server }}; domain-name {{ domain_name }}; backup-router {{ backup_router }}; root-authentication { encrypted-password "{{ root_password }}"; ## Secret data } } Here is a Jinja2 template.The configuration bits have been replaced with variables. This allows us to separate the “how” from the “what”.
  5. 5. Template data source --- host_name: tp8 dns_server: 192.168.5.68 domain_name: shermdog.com backup_router: 192.168.71.254 root_password: $1$hKWBk3fb$/ The data used to fill in the variables can come from a variety of sources such as Python dict, CSV, JSON,YAML, even databases. Here is an example of usingYAML.
  6. 6. Templates are reusable text files Templates are enhanced text files in the format you already work with. They are easily reused, shared and modified because the configuration syntax (“how”) is separated from the configuration data (“what”).
  7. 7. So why Jinja2? • Prevalent in the DevOps / NetOps community • Supported by Ansible and SaltStack • Vast amounts of information published • Easy to debug • Line numbers of exceptions directly point to the correct line in the template. • Template inheritance • Allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override. • Execution is very fast
  8. 8. Jinja2Template Basics Variables are delimited by {{ … }} and expressions (conditionals, loops, macros, blocks) by {% … %} system { host-name {{ host_name }}; name-server { {% for dns_server in dns %} {{ dns_server }}; {% endfor %} } }
  9. 9. If Statements Template: user {{ user.name }} { authentication { {% if user.passwd %} encrypted-password "{{ user.passwd }}"; {% elif user.key %} ssh-rsa "{{ user.key }}"; {% endif %} } }
  10. 10. If Statements Rendered: user rick { authentication { encrypted-password "$1$wDeov1"; } }
  11. 11. For Loops Template: {% for interface_name in core_interfaces %} {{ interface_name }} { output-traffic-control-profile CORE-MAP; } {% endfor %}
  12. 12. For Loops Rendered: xe-4/1/0 { output-traffic-control-profile CORE-MAP; } xe-4/2/0 { output-traffic-control-profile CORE-MAP; } xe-4/3/0 { output-traffic-control-profile CORE-MAP; }
  13. 13. Filters Template: {% for interface_name in core_interfaces %} {{ interface_name }} { description “{{ desc|upper }}”; } {% endfor %} Jinja2 can programmatically manipulate data directly in the template. The library includes a large amount of built in filters such as default value, upper/lower, replace, etc. http://jinja.pocoo.org/docs/dev/templates/#builtin-filters
  14. 14. Filters Rendered: xe-4/1/0 { description “UPSTREAM CON”; } xe-4/2/0 { description “DOWNSTREAM CON”; } xe-4/3/0 { description “MIRROR PORT”; }
  15. 15. Include Template: system { {% include “login.j2” %} {% include “services.j2” %} } Jinja2 templates are able to directly include the content of files (such as ssh keys) and other templates. This allows you to split a configuration into multiple pieces.
  16. 16. Include Rendered: system { login { user rick { class super-user; } services { ssh; } }
  17. 17. Block (Template Inheritance) Jinja2 provides a very powerful mechanism of template inheritance. Template inheritance allows you to build a base template that contains all the common elements / defaults of your configuration and defines blocks that child templates can override. This provides great flexibility in generating base templates and selectively changing sections based on things like role or location.
  18. 18. Block – ParentTemplate services { ssh; {% block dhcp %} dhcp { pool 0.0.0.0/0; } {% endblock dhcp %} } Here is a parent template that has wrapped the dhcp configuration in a block named ‘dhcp’. The configuration listed inside of this block is the default value used if not overridden by a child template.
  19. 19. Block – ChildTemplate {% extends "parent.j2" %} {% block dhcp %} dhcp { pool 10.0.0.0/24 { domain-name shermdog.com; } } {% endblock dhcp %} Here is a child template that contains the ‘dhcp’ block. Because it extends the parent template, the dhcp configuration inside of the block will override the parents value.
  20. 20. Block – Rendered template Rendered: services { ssh; dhcp { pool 10.0.0.0/24 { domain-name shermdog.com; } } }
  21. 21. Use templates for configurations! You will have reusable templates that are easily shared, version controlled, and updated!

×