Infrahub Templates: Streamline Infrastructure Definition and Deployment

Maintaining consistency and minimizing repetitive tasks are critical for efficiency and accuracy in the ever-evolving infrastructure management landscape. We’re thrilled to introduce a powerful new concept in Infrahub that empowers you to take charge of these challenges: Templates.

Templates in Infrahub provide the most flexible approach to defining and deploying infrastructure components. Imagine the ability to create reusable blueprints for any object within your infrastructure model. These blueprints capture standard configurations, attributes, and relationships, allowing you to rapidly provision and manage multiple instances with guaranteed uniformity.

What are Templates?

Think of Templates as master patterns for your infrastructure. They enable you to define a standard configuration for a specific object type – a server, a network switch, a virtual machine, or any other entity you manage. These templates encapsulate:

  • Node Attributes: Predefined values for standard properties like name, description, and default configurations.
  • Relationships: Pre-configured connections to other infrastructure objects, ensuring proper associations.
  • Components: Definitions for sub-elements belonging to the main object, such as server network interfaces or switch ports.

By adopting Templates, you can significantly reduce the manual effort involved in repeatedly configuring common elements, mitigate the risk of configuration drift, and ensure consistent deployments across your entire infrastructure.

Devices and More

One of the things that makes Infrahub templates different from what you’ve probably seen before is that they can create reusable blueprints for any object type, including complex relationships. NSoTs have previously offered a limited version of this capability, such as NetBox Device Types, which focused only on ensuring consistency in hardware device configurations and inventory management.

Infrahub’s templates provide greater flexibility and dynamic object creation, applicable across diverse infrastructure elements, not just devices. For example, you could use Infrahub templates for Sites or Services, allowing you to blueprint higher-level concepts in your environment easily. For example, templating Sites would enable you to have straightforward Small/Medium/Large site templates with related and common attributes populated for each Site you create in Infrahub. Alternatively, templating of Services would allow you to quickly and consistently re-create a higher-level service construct, such as an L3 VPN or a VPC and EC2 instances for an application.

How Templates Work

The process of creating and utilizing Templates involves a few key steps:

  1. Enable Template Support in Your Schema
  2. Create Templates in Infrahub
  3. Create Object Instances from Templates

Enable Template Support in Your Schema

At the core, the ability to create templates starts with your infrastructure schema definition. By enabling a specific flag at the node level for an object type, you unlock the capability to generate templates for that object and its associated component relationships.

If you are familiar with Schema Development in Infrahub, enabling template generation is straightforward. At the node level, the `generate_template` property allows users to enable template generation for a given node and its associated components.

We can now load the example template schema below into an Infrahub instance for testing. For more details, refer to the Import Schema Guide.

				
					# yaml-language-server:
$schema=https://schema.infrahub.app/infrahub/schema/latest.json
    version: "1.0"

    nodes:
      - name: Device
        namespace: Infra
        generate_template: true # Enable template for Device and its Components
        label: "Device"
        icon: "mdi:server"
        human_friendly_id: ["name__value"]
        order_by:
          - name__value
        display_labels:
          - name__value
        attributes:
          - name: name
            kind: Text
            unique: true
          - name: description
            kind: Text
            optional: true
          - name: serial
            kind: Text
            optional: true
        relationships:
          - name: device_type
            label: device_type
            peer: InfraDeviceType
            optional: false
            cardinality: one
            kind: Attribute
          - name: interfaces
            peer: InfraInterface
            optional: true
            cardinality: many
            kind: Component
				
			

Create a Template in Infrahub

Once template support is enabled, you can create specific template instances. When making a template, you can pre-fill attribute values and establish relationships. Crucially, the system automatically allows you to develop corresponding templates for component relationships, ensuring a holistic blueprint.

Consider the example of creating a template for a standard network switch model below.

  1. Create a Device Type object to hold standard information about the switch (rack units, manufacturer, etc.)
  2. Create a Device Template to use to instantiate instances of the switch.
  3. Create Interface Templates to standardize interface configurations on the switch.
  4. Create a Device instance to represent the switch itself.

Create a Device Type

Returning to the Infrahub web interface, you will notice new entries in the left-hand menu. Before entering template information, we will create a device type, which will be helpful later.

It is important to note that Infrahub’s implementation of the object template doesn’t hold any information about the device model (for example, the number of rack units). Infrahub stores this information in the device-type object. Fortunately, we can link the template to a device-type object, and Infrahub will transfer this information to the object.

Via the Web Interface
Via the GraphQL API
  1. Navigate to `Device Type` and add a new one.
  2. Fill in details like **Name**: `SwitchModel123` and **Number of U**: `2`.
  3. Save the Device Type.
				
					mutation {
  InfraDeviceTypeCreate(
    data: {name: {value: "SwitchModel123"}, number_of_u: {value: 2}}
  ) {
      ok
    }
}

				
			

Create a Device Template

All template-related records are in the menu under the dedicated section Object Management > Templates.

It is important to note that you specify the device type object in the device template. Creating a device using your template will automatically establish a relationship between the new device and the selected device type. Some fields can be left empty because the device template doesn’t apply to them. For example, the serial number will, by definition, be different from one device to another.

Via the Web Interface
  1. Go to the Templates section.
  2. Click to add a new Object Template and select `Device`.
  3. Enter Template Name: `Template-SwitchModel123` and link it to the Device Type: `SwitchModel123`.
  4. Save the template.

 

Via the GraphQL API
				
					mutation {
  TemplateInfraDeviceCreate(
    data: {template_name: {value: "Template-SwitchModel123"}, device_type: {hfid: ["SwitchModel123"]}}
  ) {
      ok
    }
}

				
			

Creating Interface Templates

These are components of the Device Template, so ensure that you reference the device template created in the previous step. You can create multiple interface templates for demonstration purposes.

Via the Web Interface
  1. Click to add a new Object Template and select `Object template Interface`.
  2. Create templates like:
    • Template Name: `Template-SwitchModel123-Ethernet1`, **Device**: `Template-SwitchModel123`, **Name**: `Ethernet1`
    •  Template Name: `Template-SwitchModel123-Ethernet2`, **Device**: `Template-SwitchModel123`, **Name**: `Ethernet2`
    •  Template Name: `Template-SwitchModel123-Ethernet3`, **Device**: `Template-SwitchModel123`, **Name**: `Ethernet3`
  3. Save the interface templates.
Via the GraphQL API
				
					mutation {
  CreateTemplateEthernet1: TemplateInfraInterfaceCreate(
    data: {name: {value: "Ethernet1"}, template_name: {value: "Template-SwitchModel123-Ethernet1"}, device: {hfid: ["Template-SwitchModel123"]}}
  ) {
      ok
    }
  CreateTemplateEthernet2: TemplateInfraInterfaceCreate(
    data: {name: {value: "Ethernet2"}, template_name: {value: "Template-SwitchModel123-Ethernet2"}, device: {hfid: ["Template-SwitchModel123"]}}
  ) {
      ok
    }
  CreateTemplateEthernet3: TemplateInfraInterfaceCreate(
    data: {name: {value: "Ethernet3"}, template_name: {value: "Template-SwitchModel123-Ethernet3"}, device: {hfid: ["Template-SwitchModel123"]}}
 ) {
     ok
   }
}

				
			

Create a Device Instance from Template

With the device and interface templates in place, you’re all set to create new instances based on them! When you need to create new instances of an object, you can now leverage the power of templates. Simply select a predefined template during the creation process. The new object will automatically inherit all the attributes and relationships the template defines, including its associated components.

Returning to the main menu, we can now create a device object. You can make your device from scratch, following the standard process, or start from a predefined template!

Via the Web Inteface

  1. Navigate to `Device` and click to add a new one.
image1
  1. Choose the “From Template” option.
  2. Select the `Template-SwitchModel123` template you created.
  3. The creation form will be pre-populated with data from the template. You can then fill in any unique information (e.g., the device’s serial number) and save it.
    • A small chip above the form inputs indicates information sourced from the template. You can override this information at any time.
image1 1

Via the GraphQL API

				
					mutation {
  InfraDeviceCreate(
    data: {
      object_template: {hfid: ["Template-SwitchModel123"]},
      serial: {value: "OWI62IUHQ"},
      description: {value: "This is a Core Switch"}
    }
 ) {
     ok
   }
}

				
			

Regardless of the method used (Web UI or GraphQL API), upon creation, the new device will have its essential attributes pre-filled and automatically have the `Ethernet1`, `Ethernet2`, and `Ethernet3` interfaces created as its components, linked according to the template definition.

When viewing your newly created device object, navigate to the Interfaces tab to see a list of interfaces pre-populated based on the template you defined.

Current Caveats

  • Currently, templates primarily support component relationships. Other types of relationships will continue to reference actual objects.
  • Modifications made to a template will not retroactively update objects previously created using that template.

Key Advantages of Using Templates

  • Boosted Productivity: With Templates, you can significantly reduce repetitive data entry and accelerate the deployment of infrastructure objects, saving you valuable time and increasing productivity.
  • Unwavering Consistency: Ensure uniformity across multiple instances of the same object type, eliminating configuration drift.
  • Minimized Errors: Templates help you reduce the likelihood of manual configuration mistakes, leading to a more stable environment and instilling confidence in your work.
  • Simplified Management: Streamline the management and deployment of standardized infrastructure components.
  • Clear Documentation: Templates serve as living documentation, providing precise blueprints for your infrastructure objects.

Try it Out!

Templates represent a significant step forward in simplifying infrastructure definition and deployment. By embracing this powerful concept, you can achieve greater efficiency, consistency, and accuracy in managing complex environments. Explore the possibilities and start building your infrastructure blueprints today!

Share the Post:

JOIN OUR MAILING LIST

Please enter your email address to stay informed about OpsMill developments. Your email address will be stored according to GDPR and will never be sold.

REQUEST A DEMO

See OpsMill in action and learn how it can help you achieve your goals. Fill out the form below to schedule a personalized demo.

By submitting this form, you agree that your personal data will be stored and processed by OpsMill in accordance with our privacy policy.