A simple NetBox integration that enables Ansible to automate devices managed by a Cisco Catalyst Center Controller.
This solution was presented by Rich Bibby at Cisco Live Amsterdam 2024 in the Cisco U. Theater. Session ID CISCOU-1014 Integrating Cisco Catalyst Center with NetBox as Part of a Modern Network Automation Solution
The four main elements of the integration are as follows:
- The NetBox data model is extended by adding two Custom Fields to the
devices
model:
-
Custom Field 1 is called
cisco_catalyst_center
, and is aSelection
type field that maps to the hostname of the Cisco Catalyst Center controller:The Custom Field makes use of a
Choice Set
calledCisco Catalyst Center Hosts
which is a drop-down menu of available Catalyst Center hosts: -
Custom Field 2 is called
ccc_device_id
, and is aText
type field that maps to the device UUID in the Cisco Catalyst Center controller
-
Devices managed by the Cisco Catalyst Center are added to NetBox and the device data includes the custom field values:
-
The NetBox Inventory Plugin for Ansible is used to dynamically generate the inventory from NetBox to be used in the Ansible playbook:
# ansible.cfg [defaults] inventory = ./netbox_inv.yml
# netbox_inv.yml plugin: netbox.netbox.nb_inventory validate_certs: False group_by: - device_roles - sites
-
The Ansible playbooks target hosts based on the
device_roles
as defined in NetBox and pulled from the dynamic inventory. They contain aset_facts
task to map the values of theccc_device_id
andcisco_catalyst_center
custom fields to the devices, so they can be used in later tasks per inventory device:--- - name: Get Device Details From Cisco Catalyst Center hosts: device_roles_distribution, device_roles_access
tasks: - name: Set Custom Fields as Facts for Cisco Catalyst Center host and Device UUID set_fact: cisco_catalyst_center: "{{ hostvars[inventory_hostname].custom_fields['cisco_catalyst_center'] }}" ccc_device_id: "{{ hostvars[inventory_hostname].custom_fields['ccc_device_id'] }}"
- name: Get Device Details uri: url: "https://{{ cisco_catalyst_center }}/dna/intent/api/v1/network-device/{{ ccc_device_id }}" method: GET return_content: yes validate_certs: no headers: Content-Type: "application/json" x-auth-token: "{{ login_response.json['Token'] }}" register: device_details delegate_to: localhost
- Clone the Git repo and change into the
netbox-ansible-cisco-cc
directory:git clone https://github.com/netboxlabs/netbox-learning.git cd netbox-learning/netbox-ansible-cisco-cc
- Create and activate Python 3 virtual environment:
python3 -m venv ./venv source venv/bin/activate
- Install required Python packages:
pip install -r requirements.txt
- Set environment variables for the NetBox API token and URL:
export NETBOX_API=<YOUR_NETBOX_URL> (note - must include http:// or https://) export NETBOX_TOKEN=<YOUR_NETBOX_API_TOKEN>
- List the devices and host variables retrieved from NetBox using the dynamic inventory:
ansible-inventory -i netbox_inv.yml --list
- Note how the Custom Fields
ccc_device_id
andcisco_catalyst_center
and their values are retrieved for each device:"sw4": { "ansible_host": "10.10.20.178", "custom_fields": { "ccc_device_id": "826bc2f3-bf3f-465b-ad2e-e5701ff7a46c", "cisco_catalyst_center": "sandboxdnac.cisco.com" },
- Run a playbook making sure to specify the NetBox dynamic inventory with the
-i
flag. For example:ansible-playbook -i netbox_inv.yml get_device_details.yml
- When you have finished working you can deactivate the Python virtual environment:
deactivate