Understanding data transformations in Infrahub
Transformations convert infrastructure data from Infrahub’s graph database into formats that external systems require. Using Jinja2 templates or Python code, transformations enable Infrahub to serve as a source of truth that generates device configurations, API payloads, documentation, and other artifacts in the exact format each system expects.Why transformations matter
Infrastructure data exists in a canonical form in Infrahub—devices, interfaces, IP addresses stored as interconnected nodes. However, external systems need specific formats:- Network devices expect vendor-specific configuration syntax
- Cloud APIs require JSON payloads with specific structure
- Documentation systems need Markdown or HTML
- Monitoring tools expect CSV or custom formats
- Ticketing systems need structured data
Core concepts
Transformation components
A transformation consists of two main components: GraphQL query: Defines the input data. The query retrieves exactly the data needed from Infrahub’s graph database:- A Jinja2 template for text-based formats (configurations, documentation)
- A Python class for complex logic or structured data (JSON, YAML)
$device_id becomes a parameter the transformation accepts.
Jinja2 transformations
Jinja2 transformations render templates using data from GraphQL queries. They excel at generating text-based formats: Device configurations: Generate vendor-specific configurations:CoreTransformJinja2) store the template path in the repository:
Python transformations
Python transformations provide full programming flexibility for complex logic or structured output:CoreTransformPython) reference the class in the repository:
Transformation parameters
Transformations inherit parameters from their GraphQL queries. This enables: Static transformations: No parameters, generate the same output each time:Architecture and implementation
Transformation execution flow
When a transformation runs:- Query execution: The GraphQL query executes against Infrahub, retrieving data from the graph database
- Data preparation: Query results are formatted and passed to the transformation logic
- Transformation: The Jinja2 template renders or Python class executes, producing output
- Result return: The output is returned to the caller (API, artifact system, CLI)
Repository integration
Transformations live in Git repositories alongside schemas, checks, and other infrastructure-as-code:.infrahub.yml file registers transformations:
CoreTransformJinja2 or CoreTransformPython objects in the database.
Transformation with groups
Transformations themselves are generic—they process data without knowing which objects they’ll transform. Targeting happens through artifact definitions and generators, which combine transformations with groups:Branch-aware transformations
Transformations are branch-aware schema objects. This means: Different transformations per branch: A feature branch can have modified transformation logic without affecting production Schema-aware: Transformations automatically adapt to schema changes in the branch Data-aware: Transformations see the branch’s data view, not the main branch When testing infrastructure changes in a branch, transformations generate artifacts using the branch’s data and schema.Implementation examples
Jinja2 transformation with filters
Jinja2 transformations support custom filters for common operations:Python transformation accessing files
Python transformations can access files in the repository:Multi-vendor transformations
A common pattern is vendor-specific transformations selected by device attribute:Transformation error handling
Transformations should handle errors gracefully:Testing transformations
Infrahub provides a testing framework for transformations with minimal configuration. Tests execute locally during development and automatically in the CI pipeline when changes affect transformations. Create test fixtures in the repository:Design trade-offs
Jinja2 vs. Python
Jinja2 transformations are simpler but less flexible. Python transformations are more powerful but more complex. Choose based on requirements: Use Jinja2 when:- Output is text-based (configurations, documentation)
- Logic is simple (loops, conditionals, basic formatting)
- Template maintainability matters (non-programmers edit templates)
- Output is structured data (JSON, YAML for APIs)
- Complex logic is required (calculations, external API calls)
- Type safety and IDE support matter
Transformation granularity
Should you create many small transformations or few large ones? Many small transformations:- Easier to test (focused scope)
- More reusable (compose into artifacts)
- Clearer purpose
- More objects to manage
- Fewer objects to manage
- May duplicate logic
- Harder to test
- Less flexible
Query complexity
GraphQL queries can fetch deeply nested data in one request, but complex queries impact performance. Balance between: Fewer complex queries: Fetch everything needed in one query- Pros: Fewer database round-trips
- Cons: Slower queries, more data transferred
- Pros: Faster individual queries
- Cons: Multiple round-trips, coordination complexity
Related topics
- Generators - Using transformations to create objects
- Artifacts - Storing transformation output
- GraphQL - Writing efficient queries
- Repository Integration - Managing transformations in Git