From a7d7016fb3d03dd43626f84f9658e00d7ee10aed Mon Sep 17 00:00:00 2001 From: medusa Date: Tue, 5 Aug 2025 23:47:21 -0500 Subject: [PATCH] Add tech_docs/jinja2_project.md --- tech_docs/jinja2_project.md | 1361 +++++++++++++++++++++++++++++++++++ 1 file changed, 1361 insertions(+) create mode 100644 tech_docs/jinja2_project.md diff --git a/tech_docs/jinja2_project.md b/tech_docs/jinja2_project.md new file mode 100644 index 0000000..6ab401d --- /dev/null +++ b/tech_docs/jinja2_project.md @@ -0,0 +1,1361 @@ +Now you have an **interactive Jinja2 playground** that brings your document to life! This lets you experiment with all the concepts from your data dump in real-time. + +## **What Makes You Dangerous with Jinja2** + +Based on your comprehensive document, you've identified the key insight: **Jinja2 is the invisible force multiplier**. Here's your tactical mastery roadmap: + +### **Level 1: Master the Fundamentals (You're Already Here)** +- ✅ Variables: `{{ variable }}` +- ✅ Loops: `{% for item in items %}` +- ✅ Conditionals: `{% if condition %}` +- ✅ Filters: `{{ value | default('fallback') }}` + +### **Level 2: Advanced Patterns (Your Next Target)** + +**A. Custom Filters for Network Engineering** +```python +def subnet_mask(cidr): + """Convert CIDR to subnet mask""" + return str(ipaddress.IPv4Network(f'0.0.0.0/{cidr}', strict=False).netmask) + +env.filters['subnet_mask'] = subnet_mask +``` +**Usage**: `{{ '24' | subnet_mask }}` → `255.255.255.0` + +**B. Macros for Reusable Components** +```jinja2 +{# Define once, use everywhere #} +{% macro cisco_interface(name, ip, vlan=None) %} +interface {{ name }} + ip address {{ ip }} +{% if vlan %} switchport access vlan {{ vlan }}{% endif %} + no shutdown +{% endmacro %} + +{# Use it #} +{{ cisco_interface('Gi0/1', '192.168.1.1', vlan=100) }} +``` + +### **Level 3: Systems Integration (Your Competitive Edge)** + +**A. NetBox + Jinja2 Pipeline** +```python +# Pull live data from NetBox +import pynetbox +nb = pynetbox.api('https://netbox.company.com', token='your-token') + +devices = [] +for device in nb.dcim.devices.all(): + devices.append({ + 'name': device.name, + 'type': str(device.device_type), + 'primary_ip': str(device.primary_ip4).split('/')[0] + }) + +# Template with live data +template.render(devices=devices) +``` + +**B. Git-Ops Automation** +```yaml +# .github/workflows/network-configs.yml +name: Generate Network Configs +on: + push: + paths: ['network-data/**'] +jobs: + generate: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Render configs + run: | + pip install jinja2-cli + jinja2 templates/cisco.j2 network-data/prod.yml > configs/prod.cfg + - name: Commit generated configs + run: | + git add configs/ + git commit -m "Auto-generated configs" +``` + +### **Level 4: Business Impact (Where You Become Irreplaceable)** + +**A. Proposal Automation (From Your Doc)** +- **Before**: 8 hours per RFP response +- **After**: 30 minutes with templated proposals +- **ROI**: 16x time multiplier + +**B. Compliance as Code** +```jinja2 +{# NIST 800-53 Control Validation #} +{% for control in nist_controls %} +## {{ control.id }}: {{ control.name }} +Status: {% if control.implemented %}✅ COMPLIANT{% else %}❌ NON-COMPLIANT{% endif %} +{% if not control.implemented %} +**Required Actions:** +{% for action in control.remediation %} +- [ ] {{ action }} +{% endfor %} +{% endif %} +{% endfor %} +``` + +### **Your Next 30 Days** + +**Week 1: Practice the Playground** +- Work through all 6 example scenarios in the artifact +- Modify each template to match your real-world data structures + +**Week 2: Build Your First Production Template** +- Choose one repetitive task (device configs, proposals, documentation) +- Create a template + data file +- Measure time savings + +**Week 3: Add Automation** +- Set up a Git repository with templates +- Add basic CI/CD to auto-generate outputs +- Share with one colleague + +**Week 4: Scale & Share** +- Create a template library for your team +- Document best practices +- Start building your reputation as "the automation expert" + +### **Pro Tips from Your Document** + +1. **Start with the pain point**: Don't template for templating's sake. Find the manual task that's eating your time. + +2. **Embrace the invisible nature**: As you noted, Jinja2 works backstage. The cleaner your final output, the more professional you look. + +3. **Multi-format mastery**: Use the same data to generate Cisco configs AND Mermaid diagrams AND Word proposals. That's your superpower. + +4. **Version everything**: Templates are code. Treat them like infrastructure. + +**Want to go deeper on any specific aspect?** Pick a scenario from the playground and I'll help you build a production-ready version with real-world data integration. + +--- + + + + + + + Jinja2 Mastery Playground + + + +
+
+

🎯 Template Editor

+ + +
+ +
+

📊 Data (YAML/JSON)

+ +
Rendered output will appear here...
+
+ +
+
+

🌐 Network Configuration

+

Generate Cisco/Juniper configs from structured data with loops and conditionals.

+
interface {{ intf.name }} + description {{ intf.desc }} +{% if intf.vlan %} switchport access vlan {{ intf.vlan }}{% endif %}
+
+ +
+

☸️ Kubernetes Manifests

+

One template → multiple environment-specific YAML manifests.

+
apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ app_name }}-{{ env }} +spec: + replicas: {{ replicas[env] }}
+
+ +
+

📋 Sales Proposals

+

Auto-generate professional RFP responses with dynamic pricing.

+
# {{ customer.name }} Proposal +Total: ${{ bom | sum(attribute='cost') }} +{% for item in bom %} +- {{ item.name }}: ${{ item.cost }}{% endfor %}
+
+ +
+

🔀 Network Diagrams

+

Generate Mermaid/Graphviz diagrams from network topology data.

+
graph TD +{% for device in devices %} + {{ device.name }}["{{ device.type }}"]{% endfor %} +{% for link in links %} + {{ link.src }} --> {{ link.dst }}{% endfor %}
+
+ +
+

🔒 Security Policies

+

Template firewall rules and compliance checks across environments.

+
{% for rule in acl_rules %} +{{ rule.action }} {{ rule.protocol }} {{ rule.src }} {{ rule.dst }} +{% endfor %}
+
+ +
+

⚡ Advanced Patterns

+

Macros, inheritance, custom filters, and complex data transformations.

+
{% macro render_vlan(vlan) %} +vlan {{ vlan.id }} + name {{ vlan.name }} +{% endmacro %}
+
+
+
+ +