def create_netbox_interface(task, nb_interfaces, netbox):
    r = task.run(task=networking.napalm_get, getters=["interfaces"])
    interfaces = r.result["interfaces"]

    for interface_name in interfaces.keys():
        if not is_interface_present(nb_interfaces, f"{task.host}", interface_name):
            print(
                f"* Creating Netbox Interface for device {task.host}, interface {interface_name}"
            )
            device_id = get_device_id(f"{task.host}", netbox)
            netbox.dcim.create_interface(
               name=f"{interface_name}",
               form_factor=1200,  # default
               device_id=device_id,
            )
def create_netbox_interfaces(task: Task, nb_interfaces: list, netbox: NetBox) -> Result:
    """Nornir task to create interfaces in Netbox obtained via napalm_get from live devices.

    Args:
        task (Task): This.
        nb_interfaces (list): List of netbox interfaces.
        netbox (NetBox): Netbox instance

    Returns:
        Result: Nornir task result
    """
    task_result = task.run(task=napalm_get, getters=['interfaces'], severity_level=logging.FATAL)
    interfaces = task_result.result['interfaces']

    changed:bool = False

    changed_items:list = []

    for interface_name in interfaces.keys():
        exists = is_interface_present(nb_interfaces=nb_interfaces,
                                    device_name=f"{task.host}",
                                    interface_name=interface_name)

        if not exists:
            print(
                f"* Creating Netbox Interface for device {task.host}, interface {interface_name}"
            )

            device_id = get_device_id(device_name=f"{task.host}", netbox=netbox)
            print (f"Device ID: {device_id}")

            dcim_result = netbox.dcim.create_interface(
                device_id=device_id,
                name=f"{interface_name}",
                interface_type="1000base-t",
                enabled=interfaces[interface_name]['is_enabled'],
                mtu=interfaces[interface_name]['mtu'],
                description=interfaces[interface_name]['description'],
                mac_address=interfaces[interface_name]['mac_address'],
            )

            changed_items.append(dcim_result)

            changed=True

    return Result(host=task.host,result=changed_items, changed=changed)
def update_netbox_interfaces(task: Task, nb_interfaces: list, netbox) -> Result:
    """Nornir task to update interfaces in Netbox obtained via napalm_get from live devices.

    Args:
        task (Task): This.
        nb_interfaces (list): List of netbox interfaces.
        netbox (NetBox): Netbox instance

    Returns:
        Result: Nornir task result
    """
    task_result = task.run(task=napalm_get, getters=['interfaces'], severity_level=logging.FATAL)
    interfaces = task_result.result['interfaces']

    changed:bool = False
    changed_items:list = []

    for interface_name in interfaces.keys():
        enabled=interfaces[interface_name]['is_enabled']
        mtu=interfaces[interface_name]['mtu']
        description=interfaces[interface_name]['description']
        mac_address=interfaces[interface_name]['mac_address']

        if (is_interface_present(nb_interfaces=nb_interfaces,
                                device_name=f"{task.host}",
                                interface_name=interface_name)):
            print(
                f"* Updating Netbox Interface for device {task.host}, interface {interface_name}"
            )

            dcim_result = netbox.dcim.update_interface(
                device=f"{task.host}",
                interface=interface_name,
                description=description,
                enabled=enabled,
                mtu=mtu,
                mac_address=mac_address,
            )

            changed=True
            changed_items.append(f"{task.host} : {interface_name} Updated: {dcim_result}")

    return Result(host=task.host, result=changed_items, changed=changed)
Exemplo n.º 4
0
def update_netbox_interface(task, nb_interfaces):
    r = task.run(task=networking.napalm_get, getters=["interfaces"])
    interfaces = r.result["interfaces"]

    for interface_name in interfaces.keys():
        mac_address = interfaces[interface_name]["mac_address"]
        if mac_address == "None" or mac_address == "Unspecified":
            mac_address = "ee:ee:ee:ee:ee:ee"

        description = interfaces[interface_name]["description"]

        if is_interface_present(nb_interfaces, f"{task.host}", interface_name):
            print(
                f"* Updating Netbox Interface for device {task.host}, interface {interface_name}"
            )
            netbox.dcim.update_interface(
                device=f"{task.host}",
                interface=interface_name,
                description=description,
                mac_address=mac_address,
            )