Skip to content

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 structure
  • file: External template files (.tftpl) using Terraform templating syntax
  • cli: 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.

NameTypeConstraintMandatoryDefault Value
templatesListtemplatesNo

NameTypeConstraintMandatoryDefault Value
nameStringYes
typeChoicemodel, file, cliYes
stageChoicepre, postNopost
orderIntegermin: 0, max: 9No0
fileStringNo
contentStringNo

NameTypeConstraintMandatoryDefault Value
nameStringYes
typeChoicemodel, file, cliYes
orderIntegermin: 0, max: 9No0
fileStringNo
contentStringNo

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 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.tftpl

Template 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 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 stage attribute to control whether they are applied before (pre) or after (post) the modeled configuration. The default stage is post. Use pre for configuration that the modeled configuration depends on.

CLI templates also support an optional order attribute (valid range: 0-9) to control the sequence in which templates are applied within a stage. 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 stage and 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 mgmt0

Example: CLI template applied before the modeled configuration (e.g., interface MTUs)

nxos:
templates:
- name: JUMBO_MTU
type: cli
stage: pre
content: |
system jumbomtu 9216

Templating 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 }

Templates can be applied at three different levels, providing flexibility in how configuration is deployed:

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_security

Templates applied at the device group level affect all devices in that group.

nxos:
device_groups:
- name: LEAF_SWITCHES
templates:
- L2_SERVICE
- L3_SERVICE

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_LOGGING

Templates are applied in the following order:

  1. Global templates
  2. Device group templates
  3. Device-specific templates

Templates at each level are merged with templates from previous levels, allowing you to build configurations incrementally.

When templates are processed, variables are resolved in the following order (highest to lowest priority):

  1. Device-level variables
  2. Device group variables
  3. Global variables
  • 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

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 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.tftpl

Template 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 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 mgmt0

Templating 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 }

Templates can be applied at three different levels, providing flexibility in how configuration is deployed:

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_security

Templates applied at the device group level affect all devices in that group.

nxos:
device_groups:
- name: LEAF_SWITCHES
templates:
- L2_SERVICE
- L3_SERVICE

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_LOGGING

Templates are applied in the following order:

  1. Global templates
  2. Device group templates
  3. Device-specific templates

Templates at each level are merged with templates from previous levels, allowing you to build configurations incrementally.

When templates are processed, variables are resolved in the following order (highest to lowest priority):

  1. Device-level variables
  2. Device group variables
  3. Global variables
  • 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