ansible_modules icon indicating copy to clipboard operation
ansible_modules copied to clipboard

[Feature]: Lookup for NetBox v3 graphql

Open ryanmerolle opened this issue 4 years ago • 2 comments

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>') }}"

ryanmerolle avatar Jul 29 '21 12:07 ryanmerolle

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.

ryanmerolle avatar Apr 08 '22 14:04 ryanmerolle

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?

bluikko avatar Apr 06 '23 06:04 bluikko