[Python] pynetbox - issue with creating devices from csv

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to use pynetbox to create devices from a csv file. I have the following:

    import os
    import csv
    import pynetbox

    from pynetbox import api

    def populate_device(dev):
    nb = pynetbox.api(
    url='<NetboxURL>',
    token='<Token>'
    )

    nb.dcim.devices.create(
    name=dev,
    device_type=89,
    role=40,
    site=9,
    )

    os.chdir('<Directory>')
    with open('<File>.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
    dev=row["name"]
    populate_device(dev)


    Which does not create a device or error out on me. I have also tried this, which also doesn't populate a device:

    os.chdir('<Directory>')
    with open('<File>.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
    try:
    nb.dcim.devices.create(
    name=row["name"],
    device_type=89,
    role=40,
    site=9,
    )
    except pynetbox.RequestError as e:
    print(e.error)


    If I run just this, then a device is created as expected:

    nb.dcim.devices.create(
    name="TEST",
    device_type=89,
    role=40,
    site=9,
    )


    With that said, it shows that I have communication end-to-end, and that the account has proper permissions to create. Not sure where this is going awry, if it is a mapping issue or a wrong variable type? I am new to this, so I expect it is an amateurish mistake.

    Continue reading...

Compartilhe esta Página