Template
Templates provide a powerful way to define reusable configuration blocks that can be applied to multiple device groups or devices with different variable values. This is particularly useful for service-related configurations that need to be deployed repeatedly across different devices or environments with slight variations.
Templates support three different types:
model: Configuration defined inline using the data model structurefile: External template files (.tftpl) using Terraform templating syntaxcli: Raw CLI commands for features not available in the data model
Configuration templates work by:
- Defining templates once
- Referencing templates by name at the global, device group, or device level
- Using global, device group, or device-level variables to populate template parameters
- Applying templates in the order specified
This approach promotes configuration consistency, reduces duplication, and simplifies management of similar services across your network infrastructure.
Diagram
Section titled “Diagram”Classes
Section titled “Classes”templates (nxos)
Section titled “templates (nxos)”| Name | Type | Constraint | Mandatory | Default Value |
|---|---|---|---|---|
| name | String | Yes | ||
| type | Choice | model, file, cli | Yes | |
| order | Integer | min: 0, max: 9 | No | 0 |
| file | String | No | ||
| content | String | No |
Template Types
Section titled “Template Types”Model Templates
Section titled “Model Templates”Model templates define configuration inline using the standard data model structure. These are best for simple, self-contained configurations.
Example:
nxos: templates: - name: L2_SERVICE type: model configuration: vlan: vlans: - id: ${vlan_id} vn_segment: 10${vlan_id} name: L2_${vlan_id} interfaces: nve: vnis: - vni: 10${vlan_id} ingress_replication_protocol: bgp evpn: vnis: - vni: 10${vlan_id} rd: auto route_target_imports: ["65000:10${vlan_id}"] route_target_exports: ["65000:10${vlan_id}"]Templating syntax can also be used within values of model templates, allowing you to reference variables directly in the configuration:
nxos: templates: - name: L2_SERVICE type: model configuration: vlan: vlans: - id: "${vlan_id}" vn_segment: 10${vlan_id} name: "L2_${site_name}_${vlan_id}" interfaces: nve: vnis: - vni: 10${vlan_id} ingress_replication_protocol: bgp evpn: vnis: - vni: 10${vlan_id} rd: auto route_target_imports: ${jsonencode(rt_imports)} route_target_exports: ${jsonencode(rt_exports)}File Templates
Section titled “File Templates”File templates reference external .tftpl files that use Terraform templating syntax. These are ideal for complex configurations with dynamic content generation.
Template Definition:
nxos: templates: - name: standard_vlans type: file file: templates/vlans.yaml.tftplTemplate File Content (templates/vlans.yaml.tftpl):
vlan: vlans:%{ for vlan in site_vlans } - id: ${vlan.id} name: "${vlan.name}"%{ endfor }Terraform Templating Syntax:
%{ }- Control structures (for loops, if statements, etc.)${ }- Variable interpolation~- Whitespace stripping
CLI Templates
Section titled “CLI Templates”CLI templates contain raw CLI commands for features that are not available in the data model. These are applied directly to the device.
CLI templates support an optional order attribute (valid range: 0-9) to control the sequence in which templates are applied. Templates with lower order values are applied first. The default order is 0, meaning templates without an explicit order value are applied first. Templates with the same order value may be applied in parallel.
Example:
nxos: templates: - name: GLOBAL_NTP type: cli order: 1 content: | ntp server 10.50.100.1 use-vrf management ntp server 10.50.100.2 use-vrf management ntp source-interface mgmt0Templating syntax can also be used within CLI templates, including control structures like loops:
nxos: templates: - name: NTP_SERVERS type: cli content: | %{ for server in ntp_servers } ntp server ${server} use-vrf management %{ endfor }Template Application Levels
Section titled “Template Application Levels”Templates can be applied at three different levels, providing flexibility in how configuration is deployed:
Global Level
Section titled “Global Level”Templates applied at the global level are inherited by all device groups and devices (unless overridden).
nxos: templates: - name: base_security type: model configuration: # ... configuration ...
# Apply to all devices global: templates: - base_securityDevice Group Level
Section titled “Device Group Level”Templates applied at the device group level affect all devices in that group.
nxos: device_groups: - name: LEAF_SWITCHES templates: - L2_SERVICE - L3_SERVICEDevice Level
Section titled “Device Level”Templates applied at the device level affect only that specific device, useful for device-specific configurations.
nxos: devices: - name: LEAF-01 url: "https://10.1.1.1" templates: - GLOBAL_NTP - GLOBAL_LOGGINGTemplate Precedence
Section titled “Template Precedence”Template Inheritance and Merging
Section titled “Template Inheritance and Merging”Templates are applied in the following order:
- Global templates
- Device group templates
- Device-specific templates
Templates at each level are merged with templates from previous levels, allowing you to build configurations incrementally.
Variable Precedence
Section titled “Variable Precedence”When templates are processed, variables are resolved in the following order (highest to lowest priority):
- Device-level variables
- Device group variables
- Global variables
Template Type Selection
Section titled “Template Type Selection”- Use model templates for straightforward configurations that map directly to the data model and only require templating of values
- Use file templates for complex configurations with loops, conditionals, or dynamic structures
- Use cli templates as a last resort, only for features not yet supported in the data model
Templates provide a way to define reusable configuration blocks that can be applied to multiple devices or device groups with different variable values, enabling parameterized and repeatable deployments. Three template types are supported: model templates that use the data model with variable substitution (${var}), file templates that reference external configuration files, and CLI templates that contain raw NX-OS CLI commands with optional ordering control. Templates are assigned to devices, device groups, or global configuration, and support an order attribute (0-9) for controlling the sequence in which multiple templates are applied.
Diagram
Section titled “Diagram”Classes
Section titled “Classes”templates (nxos)
Section titled “templates (nxos)”| Name | Type | Constraint | Mandatory | Default Value |
|---|---|---|---|---|
| name | String | Yes | ||
| type | Choice | model, file, cli | Yes | |
| order | Integer | min: 0, max: 9 | No | 0 |
| file | String | No | ||
| content | String | No |
Examples
Section titled “Examples”Example 1: Model template for L2 VXLAN service provisioning with variable substitution
nxos: templates: - name: L2_SERVICE type: model configuration: vlan: vlans: - id: ${vlan_id} vni: 10${vlan_id} name: L2_${vlan_id} interfaces: nve: vnis: - vni: 10${vlan_id} ingress_replication_protocol: bgp evpn: vnis: - vni: 10${vlan_id} rd: auto route_target_imports: ["65000:10${vlan_id}"] route_target_exports: ["65000:10${vlan_id}"]Example 2: Model template for L3 VRF service with EVPN symmetric IRB
nxos: templates: - name: L3_SERVICE type: model configuration: vrfs: - name: ${name} vni: 1${l3_vlan_id} route_distinguisher: auto address_families: - address_family: ipv4-unicast route_target_imports: ["65000:1${l3_vlan_id}"] route_target_exports: ["65000:1${l3_vlan_id}"] route_target_imports_evpn: ["65000:1${l3_vlan_id}"] route_target_exports_evpn: ["65000:1${l3_vlan_id}"] vlan: vlans: - id: ${l3_vlan_id} vni: 1${l3_vlan_id} name: ${name} interfaces: nve: vnis: - vni: 1${l3_vlan_id} associate_vrf: true vlans: - id: ${l3_vlan_id} vrf: ${name} ip_forward: trueExample 3: CLI template for custom configuration with ordering control
nxos: templates: - name: GLOBAL_NTP type: cli order: 1 content: | ntp server 10.50.100.1 use-vrf management ntp server 10.50.100.2 use-vrf management ntp source-interface mgmt0
- name: GLOBAL_LOGGING type: cli order: 2 content: | logging server 10.50.100.2 use-vrf management logging level local7 6Example 4: File-based template referencing an external configuration file
nxos: templates: - name: SECURITY_BASELINE type: file file: security_baseline.cfg order: 0