Multiple Devices
This guide builds on First Steps and shows how to extend a single-device setup to multi-device deployments using global configuration, device groups, templates and interface groups. After each change, run nac validate, nac plan, nac apply and nac test again.
Global Configuration
Section titled “Global Configuration”When managing multiple devices, it is common to have settings that apply to all of them — such as VLANs, NTP servers or syslog destinations. Rather than repeating these in every device block, you can define them once under the global key.
Add a second device and move shared configuration to global:
nxos: global: configuration: ntp: servers: - ip: 10.0.0.10 vlan: vlans: - id: 10 name: WEB
devices: - name: SWITCH1 url: https://10.1.1.1 configuration: system: hostname: SWITCH1 mtu: 9216
- name: SWITCH2 url: https://10.1.1.2 configuration: system: hostname: SWITCH2 mtu: 9216The NX-OS module merges configuration at three levels — global → device group → device — with more specific levels taking precedence. NTP and VLAN 10 are now applied to both switches without duplication. As nac validates and tests the rendered model.yaml, which contains the merged configuration of each device, nac test now verifies VLAN 10 on both switches.
Device Groups
Section titled “Device Groups”Device groups let you apply a shared configuration block to a set of devices. This is useful for anything that is common to a role (e.g., all access switches share the same spanning-tree settings).
Add a device_groups section and reference the group from each device:
nxos: global: configuration: ntp: servers: - ip: 10.0.0.10 vlan: vlans: - id: 10 name: WEB
device_groups: - name: SWITCHES configuration: spanning_tree: mode: rapid-pvst vlan: vlans: - id: 20 name: APP
devices: - name: SWITCH1 url: https://10.1.1.1 device_groups: - SWITCHES configuration: system: hostname: SWITCH1 mtu: 9216
- name: SWITCH2 url: https://10.1.1.2 device_groups: - SWITCHES configuration: system: hostname: SWITCH2 mtu: 9216Both switches now inherit the spanning_tree setting and VLAN 20 from the SWITCHES group, in addition to VLAN 10 from global. A device can belong to multiple groups, and each group is merged in order before the device-level configuration is applied.
Templates
Section titled “Templates”Templates allow you to define reusable configuration blocks that are rendered per device using variable substitution. This avoids writing nearly-identical YAML blocks for each device when only a few values differ.
The simplest template type is model — defined inline in your YAML. Add a loopback template that uses a per-device variable:
First, register the template in your YAML (you can add this to inventory.nac.yaml or a separate file):
nxos: templates: - name: LOOPBACK type: model configuration: interfaces: loopbacks: - id: 0 ip: address: ${lo0_ip}/32Then assign the template and provide the variable on each device:
nxos: device_groups: - name: SWITCHES templates: - LOOPBACK configuration: spanning_tree: mode: rapid-pvst
devices: - name: SWITCH1 url: https://10.1.1.1 device_groups: - SWITCHES variables: lo0_ip: 10.0.0.1
- name: SWITCH2 url: https://10.1.1.2 device_groups: - SWITCHES variables: lo0_ip: 10.0.0.2When Terraform runs, the LOOPBACK template is rendered once per device, substituting ${lo0_ip} with the value from each device’s variables block. SWITCH1 gets 10.0.0.1/32 and SWITCH2 gets 10.0.0.2/32.
Interface Groups
Section titled “Interface Groups”Interface groups define a named set of interface configuration that can be reused across multiple interfaces or devices. They are especially useful for enforcing consistent settings on a class of interface — for example, all loopbacks should have the same MTU and routing behaviour.
Add an interface group and reference it from the loopback template:
nxos: interface_groups: - name: LOOPBACK_INTERFACE configuration: mtu: 9216 shutdown: false
templates: - name: LOOPBACK type: model configuration: interfaces: loopbacks: - id: 0 interface_groups: [LOOPBACK_INTERFACE] ip: address: ${lo0_ip}/32The interface_groups key on an interface is a list, so a single interface can inherit from multiple groups. Group configuration is merged before any inline interface configuration is applied.
You now have the full building blocks of a Network as Code NX-OS deployment. Explore the NX-OS Data Models section for the complete list of configurable attributes, and the VXLAN example repository for a production-scale reference implementation.