Nautobot: Get IP Addresses From Nautobot
One of Nautobot's primary functions is to serve as an IPAM solution. Within that realm, the application needs to provide a method to get at IP address data for a device, quickly and easily. In this post I will review three prominent methods to get an IP address from Nautobot. It will demonstrate getting the address via:
- Nautobot REST API
- curl
- Python Requests
- GoLang HTTP
- pynautobot
- Ansible Lookup
- Nautobot GraphQL API
- curl
- Python Requests
- GoLang HTTP
- pynautobot
- Ansible Lookup
Each method I will demonstrate how to get the IP address for Loopback0 on the device bre01-edge-01
within the demo instance of Nautobot. This device has 62 interfaces, so being able to filter down to which interface IP address we are looking for makes sense.
Getting an IP Address From The API#
For this set up, the environment variables of NAUTOBOT_URL
and NAUTOBOT_TOKEN
will be used. Set those with
curl#
The first straight forward method is going to be using the curl application to accomplish the goal.
This requires the quotes to be double quotes. Bash and shell prompts will not expand it if it is single quotes.
This is the full API response that is provided then:
Python Requests#
In this example I've used the requests Session method to store the headers instead of passing it in with requests.get(). They both work, but it is good habit to utilize a session when applicable and especially when making multiple API calls. The output is:
GoLang HTTP#
This provides the same output as seen in the Python version:
pynautobot#
The Python SDK that works to turn the Nautobot API into a Python object you can get the IP address data with the code below. Pynautobot examples are falling under the REST API section at the moment as that is where it fits the best.
The execution:
REST API - Ansible#
With Ansible, there are two methods available to use. You can use the native URI module that will gather data from the API endpoint. This example is with the Nautobot Ansible collection lookup plugin, which uses pynautobot under the hood. This allows for a little easier methodology of gathering data.
Which gives the following output.
PLAYBOOK: get_ip.yml ***********************************************************************************************************************
1 plays in get_ip.yml
PLAY [GET IP ADDRESS FROM NAUTOBOT] ********************************************************************************************************
META: ran handlers
TASK [10: GET IP ADDRESS FROM NAUTOBOT] ****************************************************************************************************
task path: /home/joshv/projects/sandbox-ansible/get_ip.yml:10
ok: [localhost] => {
"ansible_facts": {
"ip_address": {
"key": "77371932-3b7f-4e94-9179-d1d4290695d9",
"value": {
"address": "10.30.128.1/32",
"assigned_object": {
"cable": null,
"device": {
"display": "bre01-edge-01",
"id": "5e7c0bdd-254b-44cb-bf7c-2f2560082f6d",
"name": "bre01-edge-01",
"url": "https://demo.nautobot.com/api/dcim/devices/5e7c0bdd-254b-44cb-bf7c-2f2560082f6d/"
},
"display": "Loopback0",
"id": "6ecee964-e4e0-4a0a-83b7-b7485633fc78",
"name": "Loopback0",
"url": "https://demo.nautobot.com/api/dcim/interfaces/6ecee964-e4e0-4a0a-83b7-b7485633fc78/"
},
"assigned_object_id": "6ecee964-e4e0-4a0a-83b7-b7485633fc78",
"assigned_object_type": "dcim.interface",
"created": "2022-11-09",
"custom_fields": {},
"description": "",
"display": "10.30.128.1/32",
"dns_name": "edge-01.bre01.mlb.nautobot.com",
"family": {
"label": "IPv4",
"value": 4
},
"id": "77371932-3b7f-4e94-9179-d1d4290695d9",
"last_updated": "2022-11-09T15:11:51.606550Z",
"nat_inside": null,
"nat_outside": null,
"notes_url": "https://demo.nautobot.com/api/ipam/ip-addresses/77371932-3b7f-4e94-9179-d1d4290695d9/notes/",
"role": null,
"status": {
"label": "Active",
"value": "active"
},
"tags": [],
"tenant": {
"display": "Nautobot Baseball Stadiums",
"id": "a39f2dd8-84c8-4816-9e6f-4a7c46e91a77",
"name": "Nautobot Baseball Stadiums",
"slug": "nautobot-baseball-stadiums",
"url": "https://demo.nautobot.com/api/tenancy/tenants/a39f2dd8-84c8-4816-9e6f-4a7c46e91a77/"
},
"url": "https://demo.nautobot.com/api/ipam/ip-addresses/77371932-3b7f-4e94-9179-d1d4290695d9/",
"vrf": null
}
}
},
"changed": false
}
TASK [debug] *******************************************************************************************************************************
task path: /home/joshv/projects/sandbox-ansible/get_ip.yml:18
ok: [localhost] => {
"msg": "10.30.128.1/32"
}
META: ran handlers
META: ran handlers
PLAY RECAP *********************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
GraphQL#
The second methodology, which is the preferred method to get data from Nautobot, as it provides the data you are looking for only, it doesn't get all of the additional data that comes with the REST API calls. The best methodology for discovering the GraphQL query is by using the iQL interface. This is by selecting GraphQL
icon on the bottom right of the Nautobot instance.
The query that is going to be used here:
On line 2 the query is indicating to search ip_addresses from Nautobot. Then to filter on the device by name and the interface by interface name. You can turn these into variables as well within the GraphQL standards. The returned response is:
Let's take a look at how this is then accomplished with the various methods.
GraphQL - Curl#
curl --location "$NAUTOBOT_URL/api/graphql/" \
--header "Content-Type: application/json" \
--header "Authorization: Token $NAUTOBOT_TOKEN" \
--data '{"query":"query {\n ip_addresses(device:\"bre01-edge-01\", interface: \"Loopback0\") {\n address\n }\n}\n","variables":{}}'
The response is back as expected, just in the printed format.
Python Requests#
The structure returned by GraphQL is a little bit different than the REST API, but nothing that we can't work through as seen on line 23.
GoLang#
pynautobot#
Pynautobot also provides a helper method to be able to make GraphQL queries as well. It returns a JSON object at .json
and can be accessed as a dictionary:
GraphQL: Ansible#
A slight modification to the Ansible playbook, adding a variable at the top for the query string, and using the action module to query instead of a lookup plugin.
With the expected result as seen:
PLAY [GET IP ADDRESS FROM NAUTOBOT] ********************************************************************************************************
TASK [10: GET IP ADDRESS FROM NAUTOBOT] ****************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
"msg": "10.30.128.1/32"
}
PLAY RECAP *********************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Summary#
There are several ways that you can get IP addresses out of Nautobot. With having the robust API capabilities that Nautobot has, including the GraphQL endpoints, you are able to work quite quickly to get at the data that you need to automate your network, automate your enterprise. Whether it is working with some programming languages, or working with an automation engine such as Ansible, Nautobot is available to help out!
Share on Share on Share on Share on Share on