Network as Code (NaC) lab
A full network-as-code workflow: model devices as variables, generate configs with Ansible Roles + Jinja2, diff against live devices, and commit-merge through a CI/CD pipeline.
Setup, key steps & run commands
Clone the repo and install the Python dependencies (Ansible, NAPALM, pyATS and friends) into a virtualenv.
# Clone the lab
git clone https://github.com/dmmar/netascode.git
cd netascode
# Install Python dependencies
pip3 install -r requirements.txtPoint Ansible at the napalm-ansible modules and action plugins so playbooks can talk to multi-vendor devices.
[defaults]
library = /usr/local/lib/python3.7/site-packages/napalm_ansible/modules
action_plugins = /usr/local/lib/python3.7/site-packages/napalm_ansible/plugins/action
# Find the exact paths on your machine with:
# napalm-ansibleBefore generating anything, verify SSH reachability to every device in an environment's inventory.
# Development environment
ansible-playbook -i inventories/development/hosts \
inventories/development/Modules/PING/ping.yaml
# Test / production use their own inventory + Modules folder
ansible-playbook -i inventories/test/hosts \
inventories/test/Modules/PING/ping.yamlEach device is described as a YAML file. group_vars holds shared values per group (e.g. Cisco), host_vars holds per-device values. Roles + Jinja2 turn these into real configs.
# inventories/development/group_vars/Cisco.yaml
ntp_servers:
- 10.0.0.123
snmp_community: lab-ro
# inventories/development/host_vars/BR1-AC1.yaml
hostname: BR1-AC1
interfaces:
- name: GigabitEthernet0/0
ip: 10.10.1.1
mask: 255.255.255.0Render full and partial configs from the variables, then diff the generated FINAL config against what is live on the device.
ansible-playbook -i inventories/development/hosts \
inventories/development/Modules/generate/generate-all-config-and-make-diff.yml
# Output lands in:
# inventories/development/CONFIGS/<DEVICE>/FINAL_pre.conf
# inventories/development/CONFIGS/<DEVICE>/FINAL_pre_DIFF.confPush the reviewed FINAL config to devices via NAPALM commit-merge — or let the GitLab CI/CD pipeline do it on merge.
ansible-playbook -i inventories/development/hosts \
inventories/development/Modules/commit-merge/commit-merge-final-config.yml
# Or trigger the same flow through .gitlab-ci.yml on merge to masterTested on Cisco IOS (IOSv, IOSvL2), Cisco vASA 9.x, JunOS (vSRX) and VyOS 1.1.8. The lab is designed to run against a GNS3 topology; see the repo for startup configs and topology images.