Mission 2: Create site hierarchy in Netbox and use it as data source for deploying to Catalyst Center
In Mission 1, we defined the site hierarchy for deployment directly within the Ansible playbook. In Mission 2, we shift our focus to dynamically managing the site and location hierarchy using NetBox as the source of truth. The task involves creating the site and location hierarchy in NetBox, retrieving the data using Ansible, formatting it appropriately, and deploying it to Catalyst Center.
Before deploying to Catalyst Center, it's crucial to establish a well-structured site and location hierarchy in NetBox. This hierarchy includes sites, regions, locations, and associated attributes such as GPS coordinates and custom fields required for the deployment. Typically, this can be done manually through the NetBox web interface. While this manual process works, it can be time-consuming, error-prone, and inefficient.
A great way to improve the input of data into Netbox are custom-scripts:
- Time-Saving: Scripts automate repetitive tasks, setting up the entire hierarchy in seconds.
- Consistency: They ensure all sites and locations follow the same structure and include the necessary fields, avoiding errors or omissions.
- Flexibility: Scripts can dynamically adjust attributes based on deployment needs.
By using custom scripts, you can focus on higher-value tasks and trust that the hierarchy is created accurately and efficiently.
NetBox provides two primary methods for using custom scripts:
- Web Interface: Upload the script directly through the NetBox web interface.
- Git Repository: Store the script in a Git repository and configure NetBox to use it from there.
Using a Git repository for custom scripts offers significant advantages. Unlike the web interface, where you must re-upload the script every time you make a change, Git allows you to:
- Easily Update the Code: Simply commit and push your changes to the repository.
- Version Control: Track changes to your scripts, roll back to previous versions, and collaborate with others seamlessly.
Step 1: Adding your Custom-script to Netbox
Step 1a: Adding your Gitlab repository as data source to Netbox
-
Log in to Netbox with the following parameters:
Key Value Netbox http://198.18.134.22 User user3 Password C1sco12345 -
Navigate to Operations > Data Sources.
-
Click on Add in the top right corner.
-
Use the following parameters to add your repository:
Key Value Name pod3 Type Git URL http://198.18.134.23/ltrops-2341/pod-3.git Username user3 Password C1sco12345 Branch master -
After adding, click on Sync in the top right corner.
-
Refresh the page. Besides seeing the status
Completed
you should also see the files from your repo synced.
Step 1b: Adding the custom script from your data source
-
In Netbox navigate to Customization > Scripts.
-
Click on Add in the top right corner.
-
Select your newly created Data source
pod3
and choose your custom-scriptnetbox/pod3_site-hierarchy.py
. Make sure to select Auto sync enabled.Example Pod-1:
-
Now you should see your custom script among the others.
Step 2: Create your site hierarchy with your custom script
-
Still in Customization > Scripts, click on your newly created custom script.
Example Pod-1:
-
Fill the form with the following data
Warning
Choose a unique
Site Name
as Netbox enforces site name uniqueness across all tenantsKey Value Tenant pod3 Region Choose region depending on your site location Site Name Feel free to choose one Address Feel free to choose one (Street, City, Country) Numbers of Floors 1 Lowest Floor 1 Commit changes true Example Pod-1:
-
Click on Run script.
-
After the script successfully executed, navigate to Organization > Sites and verify that:
- The site was created successfully
- The site (Status) is planned
- Region and Tenant are correct
-
Click on your newly created site and check if the GPS coordinates where retrieved successfully:
-
Navigate to Organization > Locations and verify that your location was created and assigned to your site.
Step 3: Run the Playbook in check-mode
As we now have our sites and locations in Netbox, we want to make sure our Ansible playbook works as expected. The main difference to Mission-1 is, that we create the configuration payload dynamically with Netbox as the source of truth. The task to create the area, sites and locations in netbox stays basically the same as in Mission-1.
-
Open the ansible playbook file
create_site-hierarchy.yml
located insideansible/playbooks
. -
You will see a lot of tasks that serve the purpose explained:
- Fetching data from Netbox
- Creating a variable named
design_sites
where our site hierarchy configuration will be stored - Appending regions, sites and locations to the
design_sites
variable - Finally creating the site hierachy in Catalyst Center as we did in Mission-1, with the payload of the
design_sites
variable. - Tasks to update the site and floor status from
planned
toactive
in Netbox when we successfully created the site in Catalyst Center.
-
We will run this playbook only in check-mode, as we will deploy to Catalyst Center later through a CI/CD pipeline. To run it type the following:
ansible-playbook ./ansible/playbooks/create_site-hierarchy.yml --check
-
Verify the content of the
design_sites
output. You should see your site as building and location as floor. Make sure GPS coordinates and country are there:Example Pod-1:
{ "design_sites": [ { "site": { "area": { "name": "EMEA", "parent_name": "Global/pod1" } }, "type": "area" }, { "site": { "building": { "address": "Richtistrasse 7, 8304 Wallisellen, Switzerland", "country": "Switzerland", "latitude": 47.409816, "longitude": 8.590415, "name": "WLSN", "parent_name": "Global/pod1/EMEA" } }, "type": "building" }, { "site": { "floor": { "floor_number": 1, "height": 10.0, "length": 100.0, "name": "WLSN-1", "parent_name": "Global/pod1/EMEA/WLSN", "rfModel": "Cubes And Walled Offices", "units_of_measure": "feet", "width": 100.0 } }, "type": "floor" } ] }