How to Query the ServiceNow Table API with Ansible
Use Ansible's uri module to query ServiceNow Table API — authenticate, filter records, handle pagination, and extract field values as variables.
April 10, 2026 · 5 min · 932 words · Rob Washington
Table of Contents
ServiceNow’s Table API is how you read and write records programmatically — incidents, change requests, service catalog items, CMDB entries. Here’s how to query it from Ansible using the uri module.
The sysparm_query parameter uses ServiceNow’s encoded query syntax:
Examples:
1
2
3
4
5
6
7
8
# Open RITMs for a specific catalog itemsysparm_query:"cat_item.name=Service Account Management^state=1"# Records created in the last 7 dayssysparm_query:"sys_created_on>javascript:gs.beginningOfLast7Days()"# Assigned to specific usersysparm_query:"assigned_to.user_name=john.smith"
For reference fields (like assigned_to), ServiceNow returns both value (sys_id) and display_value:
1
2
3
4
5
# Get the display value (human readable)item.assigned_to.display_value # "John Smith"# Get the sys_id (for API operations)item.assigned_to.value # "a1b2c3d4..."
ServiceNow limits results per page (default 10000, but often configured lower). Use sysparm_limit and sysparm_offset:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- name:Get all records with paginationansible.builtin.uri:url:"https://{{ snow_instance }}/api/now/table/sc_req_item?sysparm_query=state=1&sysparm_limit={{ page_size }}&sysparm_offset={{ offset }}"method:GETuser:"{{ snow_user }}"password:"{{ snow_password }}"force_basic_auth:trueheaders:Accept:application/jsonstatus_code:200vars:page_size:100offset:0register:page_result# Check Link header for next page- name:Check if more pages existansible.builtin.set_fact:has_next_page:"{{ 'rel=\"next\"' in (page_result.link | default('')) }}"
401 Unauthorized — wrong credentials or the user doesn’t have the rest_api_explorer role.
403 Forbidden — user authenticated but lacks read/write access to the table. Check ACLs in ServiceNow.
400 Bad Request with sysparm_query errors — your query syntax is wrong. Test it in ServiceNow’s filter builder UI first, then URL-encode it for Ansible.
Empty result set — your query is valid but returns nothing. Check sysparm_display_value=true if you need display values instead of sys_ids in reference fields.
1
2
# Add this to see human-readable values in resultsurl:"...?sysparm_display_value=true&sysparm_query=..."
The Table API covers 90% of ServiceNow automation needs. Once you have authentication working, everything else is just knowing the right table name and field names — which you can always find in ServiceNow under System Definition → Tables.
📬 Get the Newsletter
Weekly insights on DevOps, automation, and CLI mastery. No spam, unsubscribe anytime.