๐งช 5.3 Network Simulation & Test Tools
Overview
Network simulation and testing tools allow you to validate network configurations, test automation scripts, and perform regression testing without impacting production networks.
Cisco Modeling Labs (CML)
๐ฅ๏ธ Virtual Network Simulation Platform
Cisco Modeling Labs (formerly VIRL) provides a virtualized network environment for testing and development.
Key Features
- Virtual Devices: Routers, switches, firewalls, servers
- Realistic Topologies: Build complex network scenarios
- API Access: Automate lab creation and management
- Breakout Tool: Connect virtual to physical networks
Use Cases
- Test network configurations before deployment
- Develop and validate automation scripts
- Training and certification preparation
- Proof of concept demonstrations
# CML API Example (Python)
from virl2_client import ClientLibrary
# Connect to CML server
client = ClientLibrary("https://cml-server", "admin", "password", ssl_verify=False)
# Create a lab
lab = client.create_lab("Test Lab")
# Add devices
router1 = lab.create_node("R1", "iosv", 100, 100)
router2 = lab.create_node("R2", "iosv", 300, 100)
# Connect devices
lab.create_link(router1.get_interface_by_label("GigabitEthernet0/1"),
router2.get_interface_by_label("GigabitEthernet0/1"))
# Start the lab
lab.start()
print(f"Lab started: {lab.title}")pyATS & Genie
๐ Python Automated Test System
pyATS is Cisco's Python-based test automation framework, and Genie is its library for network testing and validation.
pyATS Components
| Component | Purpose |
|---|---|
| Testbed | Define devices and connections |
| Test Scripts | Define test cases |
| Genie Models | Device state parsing |
| Genie Diff | Compare configurations |
# pyATS Testbed File (testbed.yaml)
devices:
router1:
type: router
os: iosxe
credentials:
default:
username: admin
password: cisco123
connections:
cli:
protocol: ssh
ip: 192.168.1.1# pyATS Test Script
from genie.testbed import load
from genie.utils.diff import Diff
# Load testbed
testbed = load('testbed.yaml')
device = testbed.devices['router1']
device.connect()
# Learn current state
before = device.learn('interface')
# Make changes (via automation)
# ...
# Learn new state
after = device.learn('interface')
# Compare states
diff = Diff(before, after)
diff.findDiff()
print(diff)Genie Features
- Device Models: Parse CLI output into structured data
- Config Comparison: Detect configuration drift
- State Validation: Verify expected device states
- Reusable Libraries: Pre-built parsers for common commands
# Genie Parser Example
from genie.testbed import load
testbed = load('testbed.yaml')
device = testbed.devices['router1']
device.connect()
# Parse 'show interfaces' output
output = device.parse('show interfaces')
# Access structured data
for intf, data in output.items():
print(f"{intf}: {data['oper_status']}")๐ 5.4 CI/CD Pipeline in Infrastructure Automation
Overview
CI/CD (Continuous Integration/Continuous Deployment) in infrastructure automation enables automated testing, validation, and deployment of network configurations and infrastructure changes.
CI/CD Components for Infrastructure
๐ง Pipeline Components
| Component | Purpose | Tools |
|---|---|---|
| Source Control | Version control for IaC | Git, GitHub, GitLab |
| CI Server | Automate pipeline execution | Jenkins, GitLab CI, GitHub Actions |
| Testing | Validate configurations | pytest, pyATS, Robot Framework |
| Linting | Check syntax and style | yamllint, ansible-lint, tflint |
| Deployment | Apply changes | Ansible, Terraform, NSO |
| Monitoring | Track changes and health | Grafana, Prometheus, ELK |
Infrastructure CI/CD Workflow
๐ Typical Pipeline Stages
# .gitlab-ci.yml Example
stages:
- validate
- test
- deploy
validate_syntax:
stage: validate
script:
- yamllint playbooks/
- ansible-lint playbooks/
- python -m py_compile scripts/*.py
test_network:
stage: test
script:
- pytest tests/ -v
- pyats run job test_job.py
artifacts:
reports:
junit: test-results.xml
deploy_to_staging:
stage: deploy
script:
- ansible-playbook -i inventory/staging playbooks/configure_network.yml
environment:
name: staging
only:
- develop
deploy_to_production:
stage: deploy
script:
- ansible-playbook -i inventory/production playbooks/configure_network.yml
environment:
name: production
only:
- main
when: manualBenefits of CI/CD in Infrastructure
โ Key Advantages
- Automated Testing: Catch errors before production
- Consistent Deployments: Same process every time
- Faster Iterations: Quick feedback on changes
- Version Control: Track all infrastructure changes
- Rollback Capability: Revert to previous versions
- Audit Trail: Complete history of who changed what
- Reduced Risk: Test in staging before production
- Collaboration: Team reviews via pull requests
Example: Network Configuration Pipeline
๐ Automated Network Deployment
# GitHub Actions Workflow (.github/workflows/network-deploy.yml)
name: Network Configuration CI/CD
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install ansible ansible-lint yamllint pyats genie
- name: Lint YAML files
run: yamllint configs/
- name: Lint Ansible playbooks
run: ansible-lint playbooks/
test:
needs: validate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run pyATS tests
run: |
pyats run job tests/network_test_job.py --testbed testbed.yaml
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- name: Deploy to production
run: |
ansible-playbook -i inventory/production playbooks/deploy.yml๐ 5.5 Principles of Infrastructure as Code
What is Infrastructure as Code (IaC)?
Infrastructure as Code is the practice of managing and provisioning infrastructure through machine-readable definition files rather than manual configuration or interactive tools.
Core Principles of IaC
๐ฏ Fundamental Principles
| Principle | Description | Benefit |
|---|---|---|
| Version Control | Store all code in Git | Track changes, rollback, collaboration |
| Idempotency | Same result regardless of runs | Safe to re-run, no duplicates |
| Immutability | Replace, don't modify | Consistency, predictability |
| Modularity | Reusable components | DRY principle, easier maintenance |
| Testing | Validate before deployment | Catch errors early |
| Documentation | Code is self-documenting | Understand current state |
Declarative vs Imperative
๐ Two Approaches
# DECLARATIVE (Terraform)
# Define desired state
resource "cisco_interface" "gig0_1" {
name = "GigabitEthernet0/1"
description = "WAN Interface"
ip_address = "10.1.1.1"
subnet_mask = "255.255.255.0"
enabled = true
}
# Terraform figures out HOW to achieve this state# IMPERATIVE (Ansible/Python)
# Define steps to execute
- name: Configure interface
cisco.ios.ios_config:
lines:
- description WAN Interface
- ip address 10.1.1.1 255.255.255.0
- no shutdown
parents: interface GigabitEthernet0/1
# You specify WHAT to do step by stepIaC Tools Comparison
๐ ๏ธ Popular Tools
| Tool | Type | Language | Best For |
|---|---|---|---|
| Terraform | Declarative | HCL | Multi-cloud infrastructure |
| Ansible | Imperative | YAML | Configuration management |
| Python | Imperative | Python | Custom automation |
| NSO | Declarative | YANG | Network orchestration |
IaC Best Practices
โ Key Practices
- Use Version Control: All code in Git with meaningful commits
- Modularize Code: Break into reusable modules/roles
- Separate Environments: Dev, staging, production configs
- Use Variables: Avoid hardcoding values
- Document Everything: README, comments, inline docs
- Test Before Deploy: Validate syntax, run tests
- Implement CI/CD: Automate testing and deployment
- Store State Securely: Remote backends, encryption
- Use Peer Reviews: Pull requests for changes
- Follow Standards: Naming conventions, directory structure
Example: Ansible Network IaC
๐ Network Configuration as Code
# Directory Structure
network-iac/
โโโ inventory/
โ โโโ production/
โ โ โโโ hosts.yml
โ โโโ staging/
โ โโโ hosts.yml
โโโ group_vars/
โ โโโ routers.yml
โ โโโ switches.yml
โโโ roles/
โ โโโ interfaces/
โ โโโ routing/
โ โโโ security/
โโโ playbooks/
โโโ deploy_network.yml# group_vars/routers.yml --- ntp_servers: - 10.1.1.1 - 10.1.1.2 dns_servers: - 8.8.8.8 - 8.8.4.4 logging_server: 10.1.1.100
# roles/interfaces/tasks/main.yml
---
- name: Configure interfaces
cisco.ios.ios_interfaces:
config:
- name: "{{ item.name }}"
description: "{{ item.description }}"
enabled: "{{ item.enabled | default(true) }}"
loop: "{{ interfaces }}"
- name: Configure IP addresses
cisco.ios.ios_l3_interfaces:
config:
- name: "{{ item.name }}"
ipv4:
- address: "{{ item.ip }}"
loop: "{{ interfaces }}"
when: item.ip is defined# playbooks/deploy_network.yml
---
- name: Deploy Network Configuration
hosts: routers
gather_facts: no
roles:
- interfaces
- routing
- security
tasks:
- name: Save configuration
cisco.ios.ios_config:
save_when: always๐ Interactive Python Interpreter
- Write Python code in the editor
- Click "โถ Run Code" to execute
- Try sample code buttons