I am trying to interact with NetBox by using python from CentOS. What I have done is that I installed netbox with docker , so everytime I run “docker-compose up” from centOS I am able to access netbox api in my browser. I managed to add manually some new devices. So what I am trying to do now is that I want to write a python file so as to get this information that I manually added. My problem is that I cannot understand the documentation and nothing is working from those simple examples they provide in it.
The only thing that I managed to do is to retrieve the following json reply by importing the requests package:
{u'dcim': u'http://ansible.mpl.nh:2435/api/dcim/', u'circuits': u'http://ansible.mpl.nh:2435/api/circuits/', u'ipam': u'http://ansible.mpl.nh:2435/api/ipam/', u'secrets': u'http://ansible.mpl.nh:2435/api/secrets/', u'tenancy': u'http://ansible.mpl.nh:2435/api/tenancy/', u'extras': u'http://ansible.mpl.nh:2435/api/extras/', u'virtualization': u'http://ansible.mpl.nh:2435/api/virtualization/', u'plugins': u'http://ansible.mpl.nh:2435/api/plugins/'}
So what I basically do is just this:
rest_response = requests.get(url="http://ansible.../api/")
But this is just something very simple and basic. If I change the url I can get a reply from every API. How can I actually interact with NetBox and retrieving for example a device that I manually added in the API by using python ?
3
Answers
Try the official python client, it makes API accesses way easier and includes samples https://github.com/digitalocean/pynetbox
Here is an helloworld example:
The detailed API documentation is available at http://127.0.0.1:8000/api/docs/.
Here 127.0.0.1:8000 is your netbox instance.
I recommend using the pynetbox module.
Example: