Skip to content

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:

  1. Time-Saving: Scripts automate repetitive tasks, setting up the entire hierarchy in seconds.
  2. Consistency: They ensure all sites and locations follow the same structure and include the necessary fields, avoiding errors or omissions.
  3. 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:

  1. Web Interface: Upload the script directly through the NetBox web interface.
  2. 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

  1. Log in to Netbox with the following parameters:

    Key Value
    Netbox http://198.18.134.22
    User user3
    Password C1sco12345
  2. Navigate to Operations > Data Sources.

  3. Click on Add in the top right corner.

  4. 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
  5. After adding, click on Sync in the top right corner.

  6. Refresh the page. Besides seeing the status Completed you should also see the files from your repo synced.

    Example Pod-1

Step 1b: Adding the custom script from your data source

  1. In Netbox navigate to Customization > Scripts.

  2. Click on Add in the top right corner.

  3. Select your newly created Data source pod3 and choose your custom-script netbox/pod3_site-hierarchy.py. Make sure to select Auto sync enabled.

    Example Pod-1:

    Example Pod-1

  4. Now you should see your custom script among the others.

Step 2: Create your site hierarchy with your custom script

  1. Still in Customization > Scripts, click on your newly created custom script.

    Example Pod-1:

    Example Pod-1

  2. Fill the form with the following data

    Warning

    Choose a unique Site Name as Netbox enforces site name uniqueness across all tenants

    Key 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:

    Example Pod-1

  3. Click on Run script.

  4. 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
  5. Click on your newly created site and check if the GPS coordinates where retrieved successfully:

    Example WLSN

  6. 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.

  1. Open the ansible playbook file create_site-hierarchy.yml located inside ansible/playbooks.

  2. 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 to active in Netbox when we successfully created the site in Catalyst Center.
  3. 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
    
  4. 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"
          }
      ]
    }