networktocode.nautobot.lookup_graphql lookup – Queries and returns elements from Nautobot GraphQL endpoint

Note

This lookup plugin is part of the networktocode.nautobot collection (version 4.1.1).

To install it, use: ansible-galaxy collection install networktocode.nautobot. You need further requirements to be able to use this lookup plugin, see Requirements for details.

To use it in a playbook, specify: networktocode.nautobot.lookup_graphql.

New in version 1.1.0: of networktocode.nautobot

Synopsis

  • Queries Nautobot via its GraphQL API through pynautobot

Requirements

The below requirements are needed on the local controller node that executes this lookup.

  • pynautobot

Parameters

Parameter

Comments

api_version

string

added in 4.1.0 of networktocode.nautobot

The Nautobot Rest API Version to use

graph_variables

string

Dictionary of keys/values to pass into the GraphQL query

See [pynautobot GraphQL documentation](https://pynautobot.readthedocs.io/en/latest/advanced/graphql.html) for more details

query

string / required

The GraphQL formatted query string, see [pynautobot GraphQL documentation](https://pynautobot.readthedocs.io/en/latest/advanced/graphql.html).

token

string

The API token created through Nautobot

Configuration:

  • Environment variable: NAUTOBOT_TOKEN

url

string / required

The URL to the Nautobot instance to query (http://nautobot.example.com:8000)

Configuration:

  • Environment variable: NAUTOBOT_URL

validate_certs

string

Whether or not to validate SSL of the Nautobot instance

Default: “true”

Examples

# Make API Query without variables
- name: SET FACT OF STRING
  set_fact:
    query_string: |
      query {
        sites {
          id
          name
          region {
            name
          }
        }
      }

# Make query to GraphQL Endpoint
- name: Obtain list of sites from Nautobot
  set_fact:
    query_response: "{{ query('networktocode.nautobot.lookup_graphql', query=query_string, url='https://nautobot.example.com', token='<redact>') }}"

# Example with variables
- name: SET FACTS TO SEND TO GRAPHQL ENDPOINT
  set_fact:
    graph_variables:
      site_name: DEN
    query_string: |
      query ($site_name:String!) {
          sites (name: $site_name) {
          id
          name
          region {
              name
          }
          }
      }

# Get Response with variables
- name: Obtain list of devices from Nautobot
  set_fact:
    query_response: "{{ query('networktocode.nautobot.lookup_graphql', query_string, graph_variables=graph_variables,
      url='https://nautobot.example.com', token='<redact>') }}"

Return Value

Key

Description

data

dictionary

Data result from the GraphQL endpoint

Returned: success

Authors

  • Josh VanDeraa (@jvanderaa)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.