netbox-community / ansible_modules

NetBox modules for Ansible using Ansible Collections
GNU General Public License v3.0
333 stars 216 forks source link

[Feature]: nb_inventory should have an option to fetch interfaces from all virtual-chassis members #784

Open SteveRodrigue opened 2 years ago

SteveRodrigue commented 2 years ago

NetBox version

v2.11.6

Feature type

Change to existing Module

Proposed functionality

Setting a boolean to true should have the nb_inventory module to pull all interfaces from a device (master + child chassis from a virtual-chassis member).

At the moment, only direct interfaces are fetched when pulling interfaces information using nb_inventory.

Pseudo-code logic: if host is member of a virtual-chassis and virtual_chassis_all_interfaces == true then combine interfaces of all virtual-chassis members return the combined interfaces list

Use case

When doing automation of network equipment, we always target the "master/primary" chassis and all members interfaces are configured/checked through this master chassis.

We almost never target a child chassis.

Also, this change in behaviour would reproduce the Web GUI interface. In the web version of Netbox, the master/primary chassis contains all interfaces of the virtual-chassis, so we can easily see and edit information from that single view. When using the Ansible module, we can't do the same (behaviour is not consistent).

External dependencies

None that I can think of.

SteveRodrigue commented 2 years ago

Maybe this will help others. Here is my current workaround.

I created a "mini-role" that only does this:

  1. Fetch virtual-chassis information to get the members.
  2. If the host is in a VC, I loop through all members except "self" to do steps 2a and 2b. 2a. I get all interfaces from the member (I register the API call to api/dcim/interfaces/?device=member&limit=0). 2b. I merge interfaces with the register.json.results of 2a.
  3. Et voilà! All interfaces are now in interfaces.
hoalex commented 2 years ago

Would also love to see this functionality. We have several switch stacks which are modeled as a virtual chassis, and right now there's no easy way to deploy the complete interface configuration in one go, e.g. via the Ansible Cisco IOS roles.

@SteveRodrigue Would you be willing to share the code of your "mini-role"?

SteveRodrigue commented 2 years ago

Would also love to see this functionality. We have several switch stacks which are modeled as a virtual chassis, and right now there's no easy way to deploy the complete interface configuration in one go, e.g. via the Ansible Cisco IOS roles.

@SteveRodrigue Would you be willing to share the code of your "mini-role"?

@hoalex Sharing the exact role would be a bit complicated. But here is the abstracted logic...

My current not optimal logic is:

  1. Register device information from netbox: api/dcim/devices/?name={{ inventory_hostname }}
  2. Check if the device is member of a virtual_chassis: when registered_device['virtual_chassis']
  3. Get the virtual-chassis members: api/dcim/devices/?virtual_chassis_id=XXXX
  4. For all chassis members (except the master) I do a loop..., 4a.I query Netbox for their interfaces: api/dcim/interfaces/?limit=0&device=xxxx 4b. I simply merge all interfaces together into the master chassis. interfaces: {{ interfaces + member.interfaces }}
  5. Now, the list of interfaces of Master contains all interfaces.

To be honest, a streamlined logic would be this:

  1. Find the virtual_chassis ID from the master chassis.
  2. Get all interfaces for the virtual chassis: api/dcim/interfaces/?limit=0&virtual_chassis_id=XXX
  3. Replace the master interfaces variable with the list of interfaces found in step 2.