networktocode.nautobot.query_graphql module – Queries and returns elements from Nautobot GraphQL endpoint

Note

This module is part of the networktocode.nautobot collection (version 3.4.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install networktocode.nautobot.

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

New in version 1.1.0: of networktocode.nautobot

Synopsis

  • Queries Nautobot via its GraphQL API through pynautobot

Note

This module has a corresponding action plugin.

Requirements

The below requirements are needed on the host that executes this module.

  • pynautobot

Parameters

Parameter

Comments

graph_variables

dictionary

Dictionary of keys/values to pass into the GraphQL query, see (https://pynautobot.readthedocs.io/en/latest/advanced/graphql.html) for more info

query

string / required

The GraphQL formatted query string, see (https://pynautobot.readthedocs.io/en/latest/advanced/graphql.html) for more details.

token

string

The API token created through Nautobot, optional env=NAUTOBOT_TOKEN

update_hostvars

boolean

Whether or not to populate data in the in the root (e.g. hostvars[inventory_hostname]) or within the ‘data’ key (e.g. hostvars[inventory_hostname][‘data’]). Beware, that the root keys provided by the query will overwrite any root keys already present, leverage the GraphQL alias feature to avoid issues.

Choices:

  • no ← (default)

  • yes

url

string

The URL to the Nautobot instance to query (http://nautobot.example.com:8000), optional env=NAUTOBOT_URL

validate_certs

boolean

Whether or not to validate SSL of the Nautobot instance

Choices:

  • no

  • yes ← (default)

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
  networktocode.nautobot.query_graphql:
    url: http://nautobot.local
    token: thisIsMyToken
    query: "{{ query_string }}"


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

# Get Response with variables and set to root keys
- name: Obtain list of devices at site in variables from Nautobot
  networktocode.nautobot.query_graphql:
    url: http://nautobot.local
    token: thisIsMyToken
    query: "{{ query_string }}"
    variables: "{{ variables }}"
    update_hostvars: "yes"

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

data

dictionary

Data result from the GraphQL endpoint

Returned: success

graph_variables

dictionary

Variables passed in

Returned: success

query

string

Query string that was sent to Nautobot

Returned: success

url

string

Nautobot URL that was supplied for troubleshooting

Returned: success

Authors

  • Josh VanDeraa (@jvanderaa)