Package: pynautobot
PynautobotPython API client library forNautobot.Pynautobot was initially developed as a fork ofpynetbox.
Pynetbox was originally developed by Zach Moody at DigitalOcean and the NetBox Community.The complete documentation for pynautobot can be found atRead the Docs.Questions? Comments? Join us in the#nautobotSlack channel onNetwork to Code!InstallationYou can install viapiporpoetryUsing pip$pipinstallpynautobot
...Using poetry$gitclonehttps://github.com/nautobot/pynautobot.git
...
$pipinstallpoetry
...
$poetryshell
Virtualenvironmentalreadyactivated:/home/user/pynautobot/.venv
$poetryinstall
...Quick StartA short introduction is provided here; the full documention for pynautobot is atRead the Docs.To begin, import pynautobot and instantiate anApiobject, passing theurlandtoken.importpynautobotnautobot=pynautobot.api(url="http://localhost:8000",token="d6f4e314a5b5fefd164995169f28ae32d987704f",)The Api object provides access to the Apps in Nautobot.
The Apps provide access to the Models and the field data stored in Nautobot.
Pynautobot uses theEndpointclass to represent Models.
For example, here is how to accessDevicesstored in Nautobot:devices=nautobot.dcim.devicesdevices<pynautobot.core.endpoint.Endpointobjectat0x7fe801e62fa0>JobsPynautobot provides a specializedEndpointclass to represent the Jobs model. This class is calledJobsEndpoint.
This extends theEndpointclass by adding therunmethod so pynautobot can be used to call/execute a job run.Run from a instance of a job.>>>gc_backup_job=nautobot.extras.jobs.all()[14]>>>job_result=gc_backup_job.run()>>>job_result.result.id'1838f8bd-440f-434e-9f29-82b46549a31d'# <-- Job Result ID.Run with Job Inputsjob=nautobot.extras.jobs.all()[7]job.run(data={"hostname_regex":".*"})Run by providing the job id>>>gc_backup_job=nautobot.extras.jobs.run(class_path=nautobot.extras.jobs.all()[14].id)>>>gc_backup_job.result.id'548832dc-e586-4c65-a7c1-a4e799398a3b'# <-- Job Result ID.QueriesPynautobot provides several ways to retrieve objects from Nautobot.
Only theget()method is show here.
To continue from the example above, theEndpointobject returned will be used togetthe device namedhq-access-01.switch=devices.get(nam="hq-access-01")The object returned from theget()method is an implementation of theRecordclass.
This object provides access to the field data from Nautobot.switch.id'6929b68d-8f87-4470-8377-e7fdc933a2bb'switch.name'hq-access-01'switch.sitehqThreadingPynautobot supports multithreaded calls for.filter()and.all()queries. It ishighly recommendedyou haveMAX_PAGE_SIZEin your Nautobot install set to anythingexcept0orNone. The default value of1000is usually a good value to use. To enable threading, addthreading=Trueparameter when instantiating theApiobject:nautobot=pynautobot.api(url="http://localhost:8000",token="d6f4e314a5b5fefd164995169f28ae32d987704f",threading=True,)VersioningUsed for Nautobot Rest API versioning. Versioning can be controlled globally by settingapi_versionon initialization of theAPIclass and/or for a specific request e.g (list(),get(),create()etc.) by setting an optionalapi_versionparameter.Global versioningimportpynautobotnautobot=pynautobot.api(url="http://localhost:8000",token="d6f4e314a5b5fefd164995169f28ae32d987704f",api_version="1.3")Request specific versioningimportpynautobotnautobot=pynautobot.api(url="http://localhost:8000",token="d6f4e314a5b5fefd164995169f28ae32d987704f",)tags=nautobot.extras.tagstags.create(name="Tag",slug="tag",api_version="1.2",)tags.list(api_version="1.3",)Retry logicBy default, the client will not retry any operation. This behavior can be adjusted via theretriesoptional parameters. This will only affect for HTTP codes: 429, 500, 502, 503 and 504.Retriesimportpynautobotnautobot=pynautobot.api(url="http://localhost:8000",token="d6f4e314a5b5fefd164995169f28ae32d987704f",retries=3)Related projectsPlease seeour wikifor a list of relevant community projects.