Skip to main content

Retrieve data from Infrahub

Overview

The OpsMill Infrahub Ansible Collection provides two ways to interact with Infrahub GraphQL API:

  • Query Plugin: The opsmill.infrahub.query_graphql module allows you to send a structured GraphQL query and dynamically process the returned results. This method is used as an action plugin within your playbook.
  • Lookup Plugin: The opsmill.infrahub.lookup plugin enables you to retrieve GraphQL data using a query and integrate the results into your playbook (using the lookup plugin syntax).

Configuration

Before using the Infrahub plugins, you need to configure the connection to your Infrahub instance. Both plugins require:

  • Infrahub API endpoint: The URL of your Infrahub instance
  • API token: Authentication token to access the Infrahub API

You can configure these settings using environment variables or by passing them directly in your playbook.

Set these environment variables before running your playbooks:

export INFRAHUB_ADDRESS="http://localhost:8000"
export INFRAHUB_API_TOKEN="your-api-token-here"

Playbook Configuration

Alternatively, you can set these values directly in your playbook using the environment directive or module parameters:

- name: Set environment variables for Infrahub
environment:
INFRAHUB_ADDRESS: "http://localhost:8000"
INFRAHUB_API_TOKEN: "{{ lookup('ansible.builtin.env', 'INFRAHUB_API_TOKEN') }}"

Or pass them directly to the modules:

- name: Query with direct parameters
opsmill.infrahub.query_graphql:
api_endpoint: "http://localhost:8000"
token: "{{ lookup('ansible.builtin.env', 'INFRAHUB_API_TOKEN') }}"
query: "{{ query_dict }}"

Usage

query_infrahub.yml
- name: Infrahub action plugin
gather_facts: false
hosts: localhost
environment:
INFRAHUB_ADDRESS: "http://localhost:8000"
INFRAHUB_API_TOKEN: "{{ lookup('ansible.builtin.env', 'INFRAHUB_API_TOKEN') }}"

tasks:
- name: SET FACTS TO SEND TO GRAPHQL ENDPOINT
ansible.builtin.set_fact:
variables:
device_name: "atl1-edge1"
enabled: true

query_dict:
InfraInterface:
'@filters': {enabled__value: '$enabled', device__name__value: '$device_name'}
edges:
node:
id: null
name:
value: null
... on InfraInterfaceL3:
ip_addresses:
edges:
node:
address:
value: null
... on InfraInterfaceL2:
tagged_vlan:
edges:
node:
vlan_id:
value: null
name:
value: null
untagged_vlan:
node:
vlan_id:
value: null
name:
value: null

- name: Execute Query Plugin
opsmill.infrahub.query_graphql:
query: "{{ query_dict }}"
graph_variables: "{{ variables }}"
  • The query_dict defines a GraphQL query structure.
  • The query filters devices by name and interfaces by enabled.
  • The opsmill.infrahub.query_graphql plugin executes the query and returns the results.

To run the query playbook, use:

ansible-playbook query_infrahub.yml

This executes the query and fetches data dynamically from Infrahub.

Example Output from Query plugin
ok: [localhost] => {
"msg": {
"edges": [
{
"node": {
"id": "18219d8a-5417-acae-2dbc-c5156eefb194",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "10.1.0.0/31"
}
}
}
]
},
"name": {
"value": "Ethernet1"
}
}
},
{
"node": {
"id": "18219d8b-afcc-3d1b-2db4-c510fcc52b8a",
"ip_addresses": {
"edges": []
},
"name": {
"value": "Ethernet10"
}
}
},
{
"node": {
"id": "18219d8b-b7bc-dc82-2dba-c51741f8258e",
"name": {
"value": "Ethernet11"
},
"tagged_vlan": {
"edges": []
},
"untagged_vlan": {
"node": {
"name": {
"value": "atl1_server"
},
"vlan_id": {
"value": 200
}
}
}
}
},
{
"node": {
"id": "18219d8b-d71d-e7d1-2dba-c515e35ae516",
"name": {
"value": "Ethernet12"
},
"tagged_vlan": {
"edges": []
},
"untagged_vlan": {
"node": {
"name": {
"value": "atl1_server"
},
"vlan_id": {
"value": 200
}
}
}
}
},
{
"node": {
"id": "18219d8a-7e5a-df0a-2db2-c51e9967f805",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "10.1.0.2/31"
}
}
}
]
},
"name": {
"value": "Ethernet2"
}
}
},
{
"node": {
"id": "18219d8a-8673-961c-2db0-c5179da68d60",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "10.1.0.20/31"
}
}
}
]
},
"name": {
"value": "Ethernet3"
}
}
},
{
"node": {
"id": "18219d8a-8a01-a4b8-2db8-c51c7024f9dd",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "10.1.0.24/31"
}
}
}
]
},
"name": {
"value": "Ethernet4"
}
}
},
{
"node": {
"id": "18219d8a-8d42-143c-2dbf-c51a0cc428a9",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "203.111.0.1/29"
}
}
}
]
},
"name": {
"value": "Ethernet5"
}
}
},
{
"node": {
"id": "18219d8b-63d4-9267-2db4-c518a51761cf",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "203.111.0.9/29"
}
}
}
]
},
"name": {
"value": "Ethernet6"
}
}
},
{
"node": {
"id": "18219d8b-8e96-24cf-2db4-c51ba3de6b92",
"ip_addresses": {
"edges": []
},
"name": {
"value": "Ethernet7"
}
}
},
{
"node": {
"id": "18219d8b-921b-2dd9-2dbb-c519044a4a98",
"ip_addresses": {
"edges": []
},
"name": {
"value": "Ethernet8"
}
}
},
{
"node": {
"id": "18219d8b-95ce-829d-2dbd-c519355daf70",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "203.111.0.17/29"
}
}
}
]
},
"name": {
"value": "Ethernet9"
}
}
},
{
"node": {
"id": "18219d89-dbf4-92da-2dbd-c51e7009abd0",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "10.0.0.1/32"
}
}
}
]
},
"name": {
"value": "Loopback0"
}
}
},
{
"node": {
"id": "18219d8a-3900-2637-2db7-c511a30b601a",
"ip_addresses": {
"edges": [
{
"node": {
"address": {
"value": "172.16.0.1/16"
}
}
}
]
},
"name": {
"value": "Management0"
}
}
},
{
"node": {
"id": "18219d8b-dfe6-4944-2db9-c511dc191dc6",
"name": {
"value": "port-channel1"
}
}
}
]
}
}

Conclusion

Both the Query Plugin and Lookup Plugin allow you to perform GraphQL queries against Infrahub, but they are tailored to different usage patterns in your Ansible playbooks. Use the Query Plugin when you want to execute complex, structured queries with variables as an action, and the Lookup Plugin when you prefer a simpler query string approach for integrating query results directly into variables.