[Feature]: Lookup for NetBox v3 graphql
ISSUE TYPE
- Feature Idea
SOFTWARE VERSIONS
v3.1.1
Ansible:
2.10.12
Netbox:
NetBox v3.0-beta1
Collection:
SUMMARY
With graphQL read only API being integrated into NetBox, it only makes sense to create a lookup plugin.
SUGGESTED PARAMETERS
Following the standard Ansible module documentation:
| Parameter | Choices/Defaults | Configuration | Comments |
|---|---|---|---|
| api_endpoint (string / required) | env:NETBOX_API / env:NETBOX_URL | ||
| key_file (string) | |||
| token (string) | env:NETBOX_TOKEN / env:NETBOX_API_TOKEN | ||
| validate_certs (string) | Default: "yes" | Whether or not to validate SSL of the NetBox instance | |
| query (string / required) | The GraphQL formatted query string | ||
| variables (string) | Dictionary of keys/values to pass into the GraphQL query |
SUGGESTED RETURN VALUES
| Key | Returned | Description |
|---|---|---|
| data (dictionary) | success | Data result from the GraphQL endpoint |
EXAMPLE USAGE
# Make API Query without variables
- name: SET FACT OF STRING
set_fact:
query_string: |
query {
site_list {
id
name
region {
name
}
}
}
# Make query to GraphQL Endpoint
- name: Obtain list of sites from NetBox
set_fact:
query_response: "{{ query('netbox.netbox.lookup_graphql', query=query, url='https://netbox.example.com', token='<redact>') }}"
# Example with variables
- name: Set Facts to send to graphQL endpoint
set_fact:
graph_variables:
site_name: NY
query_string: |
query ($site_name:String!) {
device_list(site: $site_name) {
id
name
}
}
# Get Response with variables
- name: Retrieve a list of devices from graphql
set_fact:
query_response: "{{ query('netbox.netbox.lookup_graphql', query_string, variables=graph_variables, url='https://netbox.example.com', token='<redact>') }}"
Recently I came across nautobot ansible variable management at scale.
It leverages pynautobot's graphql interface. I think it would be good to minimize unneeded hostvars taking up resources until needed to shrink overall resource requirements and decrease run time. I will look to link to a pynetbox issue. It seems someone already submitted a PR.
Nautobot has implemented this: https://github.com/nautobot/nautobot-ansible/blob/develop/plugins/lookup/lookup_graphql.py
It is GPLv3 licensed, I wonder if this could be implemented here? Or a cleanroom re-implementation/totally independent plugin is needed?