Upload
khangminh22
View
4
Download
0
Embed Size (px)
Citation preview
Alexey (Oleksii) BorisenkoDevNet deloper advocate@alex_dev_k
DEVWKS-1512
Using YAML and Jinja Templates to Create Reusable Code
Becoming the Jinja Ninja
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
• Introduction
• Reading YAML Formatted Data
• Working with Jinja Templates
• Pulling it All Together
• Wrap-Up
Agenda
DEVWKS-1512
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
The Foundation for Reusable Code
• Scripting Language• Ex: Python, Ansible, NSO
• Templates – Sets of re-usable configuration and operational command sets. Include logic for substitution of device specific parameters • Ex: Jinja2, TextFSM, Notepad J
• Structured Data – Any data that exists in a fixed field within a record or a file• Ex: Dictionaries, Lists, CSV, YAML
DEVWKS-1512
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
A human readable data structure that
applications use to store, transfer, and read data.
YAML - “YAML Ain’t Markup Language”
DEVWKS-1512
---ietf-interfaces:interface:name: GigabitEthernet2description: Wide Area Networkenabled: trueietf-ip:ipv4:address:- ip: 172.16.0.2netmask: 255.255.255.0
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
• Jinja2 is one of the most used template engines for Python.
• Extends capabilities through tools such as loops, conditionals and inheritance.
• Installed via pip
• http://jinja.pocoo.org
Jinja Templating
DEVWKS-1512
{% for member in band_member -%}
{% if member.instrument == "Singer" -%}{{ member.name }} Ramone is the{{ member.instrument }}
{% else -%}{{ member.name }} Ramone plays the{{ member.instrument }}
{% endif -%}
{% endfor -%}
Jinja Templating – Loops and Conditionals
Johnny, GuitarDee Dee, Bass Joey, SingerTommy, Drums
Johnny Ramone plays the GuitarDee Dee Ramone plays the BassJoey Ramone is the SingerTommy Ramone plays the Drums
Input: Output:
• Variables and/or logic are placed between delimiters• {% …. %} - Used for expressions or loops.
• ‘-%}’ – Removes additional white space.
• {{ …. }} – Used for outputting the expression or variable.
• Loops• Opened with {% for ‘x’ in ‘y’ %} and typically fed in
from some form of programmatic script.• Closed with {% endfor %}
• Conditionals• Can be nested in other operations• if, elif, else otpions (ex: {% if x == y %}• Closed with {% endif %}
{% for input in inputs -%}
{% if input.name == "Tyrion" -%}
"{{ input.motto }}" motto of {{ input.house }}
{% elif input.name == "Jon Snow" -%}
"{{ input.motto }}" motto of {{ input.house }}
{% else -%}
"{{ input.motto }}" motto of {{ input.house }}
{% endif -%}
{% endfor -%}
Jinja Templating – Loops and Conditionals
"Hear Me Roar!, A Lannister Always Pays His Debts" motto of House Lannister"Winter Is Coming" motto of House Stark"Fire And Blood" motto of House Targaryen
Output:
• Variables and/or logic are placed between delimiters• {% …. %} - Used for expressions or loops.
• ‘-%}’ – Removes additional white space.
• {{ …. }} – Used for outputting the expression or variable.
• Loops• Opened with {% for ‘x’ in ‘y’ %} and typically fed in
from some form of programmatic script.• Closed with {% endfor %}
• Conditionals• Can be nested in other operations• if, elif, else otpions (ex: {% if x == y %}• Closed with {% endif %}
Questions? Use Cisco Webex Teams to chat with the speaker after the session
Find this session in the Cisco Events Mobile AppClick “Join the Discussion”Install Webex Teams or go directly to the team spaceEnter messages/questions in the team space
How1234
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
cs.co/ciscolivebot#
Cisco Webex Teams
13DEVWKS-1512
DEVWKS-1512