networktocode.nautobot.query_graphql (5.1.1) — module

Queries and returns elements from Nautobot GraphQL endpoint

| "added in version" 1.1.0 of networktocode.nautobot"

Authors: Josh VanDeraa (@jvanderaa)

This plugin has a corresponding action plugin.

Install collection

Install with ansible-galaxy collection install networktocode.nautobot:==5.1.1


Add to requirements.yml

  collections:
    - name: networktocode.nautobot
      version: 5.1.1

Description

Queries Nautobot via its GraphQL API through pynautobot


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Make API Query without variables
  - name: SET FACT OF STRING
    set_fact:
      query_string: |
        query {
          locations {
            id
            name
            parent {
              name
            }
          }
        }
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Make query to GraphQL Endpoint
  - name: Obtain list of locations from Nautobot
    networktocode.nautobot.query_graphql:
      url: http://nautobot.local
      token: thisIsMyToken
      query: "{{ query_string }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.

  # Example with variables
  - name: SET FACTS TO SEND TO GRAPHQL ENDPOINT
    set_fact:
      graph_variables:
        $location_name: AMS01
      query_string: |
        query ($location_name: String!) {
          locations (name: $location_name) {
            id
            name
            parent {
                name
            }
          }
        }
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Get Response with variables and set to root keys
  - name: Obtain list of devices at location in variables from Nautobot
    networktocode.nautobot.query_graphql:
      url: http://nautobot.local
      token: thisIsMyToken
      query: "{{ query_string }}"
      graph_variables: "{{ graph_variables }}"
      update_hostvars: yes

Inputs

    
url:
    description:
    - The URL to the Nautobot instance to query (http://nautobot.example.com:8000), optional
      env=NAUTOBOT_URL
    required: false
    type: str

query:
    description:
    - The GraphQL formatted query string, see (U(https://pynautobot.readthedocs.io/en/latest/advanced/graphql.html))
      for more details.
    required: true
    type: str

token:
    description:
    - The API token created through Nautobot, optional env=NAUTOBOT_TOKEN
    required: false
    type: str

api_version:
    description:
    - API Version Nautobot REST API
    required: false
    type: str

validate_certs:
    default: true
    description:
    - Whether or not to validate SSL of the Nautobot instance
    required: false
    type: bool

graph_variables:
    default: {}
    description:
    - Dictionary of keys/values to pass into the GraphQL query, see (U(https://pynautobot.readthedocs.io/en/latest/advanced/graphql.html))
      for more info
    required: false
    type: dict

update_hostvars:
    default: false
    description:
    - 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.
    required: false
    type: bool

Outputs

data:
  description:
  - Data result from the GraphQL endpoint
  returned: success
  type: dict
graph_variables:
  description:
  - Variables passed in
  returned: success
  type: dict
query:
  description:
  - Query string that was sent to Nautobot
  returned: success
  type: str
url:
  description:
  - Nautobot URL that was supplied for troubleshooting
  returned: success
  type: str