You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Network automation depends on separating data (what you want to configure) from logic (how to apply it). Data formats like JSON and YAML store device variables, while template engines like Jinja2 generate device-specific configurations from those variables.
| Approach | Problem |
|---|---|
| Hardcoded configs | Every device needs a separate script; changes require editing code |
| Data + Template | Write the template once, feed in different data for each device |
Data (YAML) + Template (Jinja2) = Device Config
───────────── ────────────── ──────────────
hostname: R1 hostname {{ hostname }} hostname R1
mgmt_ip: 10.0.0.1 interface Mgmt0 interface Mgmt0
ip address {{ mgmt_ip }} ip address 10.0.0.1
JSON is a lightweight, text-based data format widely used by APIs and automation tools:
| Rule | Example |
|---|---|
| Data is in key-value pairs | "hostname": "R1" |
| Strings use double quotes | "name": "switch01" |
| Numbers have no quotes | "vlan_id": 100 |
| Booleans are lowercase | "enabled": true |
| Arrays use square brackets | ["Gi0/0", "Gi0/1"] |
| Objects use curly braces | {"hostname": "R1"} |
| No trailing commas | Last item has no comma |
| No comments allowed | Use a separate field if needed |
{
"devices": [
{
"hostname": "core-rtr-01",
"ip": "10.0.0.1",
"platform": "cisco_ios",
"site": "London",
"interfaces": [
{
"name": "GigabitEthernet0/0",
"ip": "10.0.0.1",
"mask": "255.255.255.252",
"description": "Uplink to ISP"
},
{
"name": "GigabitEthernet0/1",
"ip": "10.0.1.1",
"mask": "255.255.255.0",
"description": "LAN segment"
}
]
}
]
}
YAML is a human-friendly data format favoured by automation tools like Ansible:
| Rule | Example |
|---|---|
| Key-value pairs use colon-space | hostname: R1 |
| Indentation defines structure (spaces, not tabs) | Two-space indent is standard |
| Lists use a dash prefix | - GigabitEthernet0/0 |
| Strings usually need no quotes | hostname: switch01 |
Comments use # | # This is a comment |
Booleans: true / false | enabled: true |
| Multi-line strings use ` | (literal) or>` (folded) |
devices:
- hostname: core-rtr-01
ip: 10.0.0.1
platform: cisco_ios
site: London
interfaces:
- name: GigabitEthernet0/0
ip: 10.0.0.1
mask: 255.255.255.252
description: Uplink to ISP
- name: GigabitEthernet0/1
ip: 10.0.1.1
mask: 255.255.255.0
description: LAN segment
| Feature | JSON | YAML |
|---|---|---|
| Readability | Moderate (brackets everywhere) | High (clean, minimal syntax) |
| Comments | Not supported | Supported (#) |
| Used by | APIs, JavaScript, config files | Ansible, Kubernetes, CI/CD |
| Strictness | Strict (quotes required for strings) | Flexible (quotes often optional) |
| Parsing speed | Fast | Slightly slower |
| Human editing | Harder with large files | Easier |
Tip: Use YAML for files that humans edit (inventories, playbooks). Use JSON for API responses and machine-generated data.
Jinja2 is a powerful template engine for Python. In network automation, it generates device configurations from templates and variables:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.