Infrastructure as Code for Cisco IOS XE devices using reusable components
This project provides a modular Terraform configuration for managing Cisco routers via RESTCONF API. Configure VLANs, interfaces, and network segments with validated, reusable components.
- Cisco IOS XE device with RESTCONF enabled
- Network connectivity to device management interface
- Valid admin credentials
- Terraform >= 1.0 installed
Enable RESTCONF on your Cisco device:
configure terminal
restconf
interface GigabitEthernet0/0/1
ip address 192.168.1.1 255.255.255.0
no shutdown
exit
ip http server
ip http secure-server
username admin privilege 15 secret cisco123
git clone <your-repo>
cd cisco_terraform
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your device credentials
Use the main configuration file for simple setups:
# 1. Configure your credentials
nano terraform.tfvars
# 2. Plan and apply changes
terraform init
terraform plan
terraform apply
Configure individual ports for isolated, reliable deployments:
# 1. Choose a port to configure
cd ports/port-gi0-1-0 # Internal LAN example
# 2. Review and edit configuration
nano terraform.tfvars # Edit VLAN, IP addresses, etc.
# 3. Deploy the port
terraform init
terraform plan
terraform apply
Configure multiple ports simultaneously:
# Deploy all LAN ports in parallel
cd ports
for port in port-gi0-1-*; do
(cd "$port" && terraform apply -auto-approve) &
done
wait
graph TB
subgraph "Project Structure"
A[main.tf<br/>Complete Router Config]
B[components/<br/>Reusable Components]
C[ports/<br/>Individual Port Configs]
D[shared/<br/>Common Configuration]
end
subgraph "Components"
B --> E[interface/]
B --> F[vlan/]
B --> G[network/]
B --> H[port-config/]
B --> I[provider/]
end
subgraph "Port Configurations"
C --> J[port-gi0-1-0/<br/>Internal LAN]
C --> K[port-gi0-1-1/<br/>DMZ]
C --> L[port-gi0-1-2/<br/>Guest Network]
C --> M[port-gi0-1-3/<br/>Server Network]
end
A --> B
C --> B
C --> D
style A fill:#e1f5fe
style B fill:#f3e5f5
style C fill:#e8f5e8
style D fill:#fff3e0
flowchart TD
A[Start: Need to Configure Router] --> B{What type of change?}
B -->|Simple/Learning| C[Use main.tf]
B -->|Production/Complex| D[Use port-specific configs]
B -->|New Component| E[Create custom component]
C --> F[Edit terraform.tfvars]
D --> G[Choose port directory]
E --> H[Use components/ templates]
F --> I[terraform plan]
G --> J[Edit port terraform.tfvars]
H --> K[Test component]
J --> L[terraform plan for port]
I --> M{Plan looks good?}
L --> N{Port plan looks good?}
K --> O{Component works?}
M -->|No| F
M -->|Yes| P[terraform apply]
N -->|No| J
N -->|Yes| Q[terraform apply for port]
O -->|No| H
O -->|Yes| R[Document and share]
P --> S[β
Router Configured]
Q --> T[β
Port Configured]
R --> U[β
Component Ready]
style A fill:#e3f2fd
style S fill:#c8e6c9
style T fill:#c8e6c9
style U fill:#c8e6c9
# Using main.tf approach
nano main.tf # Add new module block
terraform plan && terraform apply
# Using port-specific approach
cd ports/port-gi0-1-0
nano terraform.tfvars # Change vlan_id and vlan_name
terraform plan && terraform apply
cd ports/port-gi0-1-1 # DMZ example
nano terraform.tfvars
# Edit physical_interface_ip and vlan_interface_ip
terraform plan && terraform apply
# Copy existing port configuration
cp -r ports/port-gi0-1-0 ports/port-gi0-1-4
cd ports/port-gi0-1-4
nano terraform.tfvars # Configure new values
terraform init && terraform apply
graph LR
subgraph "Cisco Router"
A[GigE0/0/1<br/>Management<br/>192.168.1.1/24]
subgraph "Data Ports"
B[GigE0/1/0<br/>VLAN 10<br/>Internal LAN<br/>10.10.10.0/24]
C[GigE0/1/1<br/>VLAN 20<br/>DMZ<br/>10.20.20.0/24]
D[GigE0/1/2<br/>VLAN 30<br/>Guest<br/>10.30.30.0/24]
E[GigE0/1/3<br/>VLAN 40<br/>Servers<br/>10.40.40.0/24]
end
end
A --> F[Internet/WAN]
B --> G[Corporate Network]
C --> H[Public Services]
D --> I[Guest Users]
E --> J[Server Farm]
style A fill:#ffeb3b
style B fill:#4caf50
style C fill:#ff9800
style D fill:#2196f3
style E fill:#9c27b0
cisco_terraform/
βββ π main.tf # Complete router configuration
βββ π terraform.tfvars # Main configuration values
βββ π components/ # Reusable components (DRY)
β βββ π interface/ # Physical interface configuration
β βββ π vlan/ # VLAN management
β βββ π network/ # Complete network segments
β βββ π port-config/ # High-level port abstraction
β βββ π README.md # Component documentation
βββ π ports/ # Individual port configurations
β βββ π port-gi0-1-0/ # Internal LAN (VLAN 10)
β βββ π port-gi0-1-1/ # DMZ (VLAN 20)
β βββ π port-gi0-1-2/ # Guest Network (VLAN 30)
β βββ π port-gi0-1-3/ # Server Network (VLAN 40)
βββ π shared/ # Common configuration files
βββ π versions.tf # Terraform version requirements
βββ π provider.tf.template # Provider configuration template
βββ π variables.tf # Standard variables
module "new_interface" {
source = "./components/interface"
interface_type = "GigabitEthernet"
interface_name = "0/2/0"
description = "New Interface"
shutdown = false
ipv4_address = "192.168.50.1"
}
module "new_network" {
source = "./components/network"
vlan_id = 50
vlan_name = "New_Network"
interface_name = "0/2/0"
interface_description = "New Network Segment"
network_cidr = "10.50.50.0/24"
physical_interface_ip = "10.50.50.1"
vlan_interface_ip = "10.50.50.254"
}
# Test device connectivity
ping 192.168.1.1
# Verify RESTCONF is enabled
curl -k https://192.168.1.1/restconf/data/ietf-yang-library:modules-state
# Check configuration syntax
terraform validate
# See detailed plan
terraform plan -detailed-exitcode
# Force refresh state
terraform refresh
# Clean and reinitialize
rm -rf .terraform terraform.tfstate*
terraform init
# Check current device configuration
terraform show
# Import existing resources
terraform import iosxe_vlan.example 10
# Remove resource from state (danger!)
terraform state rm iosxe_vlan.example
- Never commit credentials to git
- Use environment variables for sensitive data:
export TF_VAR_esr_username="admin" export TF_VAR_esr_password="your-password"
- Enable device logging for audit trails
- Use separate state files for production
- Test changes in development environment first
Component | Purpose | Use When |
---|---|---|
interface/ |
Configure physical interfaces | Need basic interface setup |
vlan/ |
Manage VLANs and VLAN interfaces | Working with VLANs only |
network/ |
Complete network segments | Setting up full network segment |
port-config/ |
High-level port configuration | Want validation and safety |
- Adding new components: Follow the structure in
components/
- Testing: Validate with
terraform validate
andterraform plan
- Documentation: Update this README and component docs
- Examples: Add working examples to help others
- Documentation: Check
components/README.md
for component details - Examples: See working configurations in
ports/
directory - Issues: Review terraform plan output for detailed error messages
π Ready to configure your router? Start with the Quick Start section above!