netbox-community / ansible_modules

NetBox modules for Ansible using Ansible Collections
GNU General Public License v3.0
334 stars 216 forks source link

[Feature]: Lookup for NetBox v3 graphql #566

Open ryanmerolle opened 3 years ago

ryanmerolle commented 3 years ago
ISSUE TYPE
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 The URL to the Netbox instance to query
key_file (string) The location of the private key tied to user account.
token (string) env:NETBOX_TOKEN / env:NETBOX_API_TOKEN The API token created through Netbox. This may not be required depending on the Netbox setup.
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 commented 2 years ago

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.

bluikko commented 1 year ago

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?