First Steps
This guide walks you through configuring a Cisco NX-OS switch using Network as Code — starting from a single YAML file and a one-line Terraform module. The whole workflow is driven by the nac CLI, which wraps Terraform/OpenTofu, nac-validate and nac-test.
Prerequisites:
- uv, used to install
nacand to runnac-validateandnac-test - OpenTofu or Terraform 1.9.0 or later. If neither is installed,
nac setupcan download OpenTofu for you. - Your preferred text editor / IDE (such as Visual Studio Code)
- NX-OS device reachable from your workstation
-
Install the
Section titled “Install the nac CLI”nacCLInacis published on PyPI asnac-cli:Terminal window uv tool install nac-cli -
Enable NX-API on Devices
Section titled “Enable NX-API on Devices”NX-API must be enabled on each NX-OS device before Terraform can communicate with it. SSH into each device and run:
feature nxapiEnsure the device management IP is reachable from your workstation.
-
Set Environment Variables
Section titled “Set Environment Variables”The NX-OS provider and the tests authenticate using environment variables. Set the following in your terminal:
Terminal window export NXOS_USERNAME=adminexport NXOS_PASSWORD=password -
Create Your First Data File
Section titled “Create Your First Data File”In a new project directory, create a file named
inventory.nac.yamlwith the following content. Replace theurlwith your device’s management address:nxos:devices:- name: SWITCH1url: https://10.1.1.1configuration:system:hostname: SWITCH1mtu: 9216vlan:vlans:- id: 10name: WEBThis file describes the desired state of a single switch: its hostname, system MTU and a VLAN. The
configurationblock uses the same structure as the NX-OS CLI, so you can describe any setting you would normally configure via the command line. -
Create the Terraform Configuration
Section titled “Create the Terraform Configuration”In the same directory, create a file named
main.tf:module "nxos" {source = "netascode/nac-nxos/nxos"version = "0.4.1"yaml_files = ["inventory.nac.yaml"]}This is the only Terraform configuration required. The
nac-nxosmodule reads the YAML file and maps its contents to the underlying NX-OS provider resources automatically. It also writes the rendered data model tomodel.yaml, which is the file thatnacvalidates and tests. -
Set Up and Initialize
Section titled “Set Up and Initialize”Verify the prerequisites, then download the provider and module:
Terminal window nac setupnac init -
Validate the Data Model
Section titled “Validate the Data Model”Terminal window nac validateThis renders
model.yamland validates it against the schema and rules packaged with the module, catching errors such as typos or invalid values before anything is sent to the device. -
Plan and Apply the Configuration
Section titled “Plan and Apply the Configuration”Terminal window nac plannac applynac plansaves the changes required to reach the desired state toplan.tfplan, andnac applypushes exactly this plan to the device via NX-API. -
Test the Deployment
Section titled “Test the Deployment”Terminal window nac testThis runs the tests packaged with the module against the device and verifies that the configuration in
model.yamlwas applied, for example that VLAN 10 exists with the nameWEB. The results, including an HTML report, are written totests/results/.
Congratulations! You have configured and tested your first NX-OS device using Network as Code. Continue with Multiple Devices to learn how to scale this to multi-device deployments using global configuration, device groups, templates and interface groups.