Have you ever had to query and remove a long list of ServiceNow records? To set up the environment, I use a demo system and another workflow to create a random user and then allow a learner to progress through some challenges using full Red Hat Ansible Automation Platform deployments and a shared ServiceNow instance.

For this reason, I recently had to develop some automation to clean up records created by these demo user accounts. Although my use-case was to clean up demo user accounts, this could just as well have been a critical ServiceNow instance that had erroneous records that needed cleaning up.

For instance, I can query and close all active incident records created by a specific user: - name: find user created incidents servicenow.itsm.incident_info: query: - sys_created_by: = {{ username }} active: = true register: incidents - name: query incident number and creation time ansible.builtin.set_fact: incident_list: '{{ incident_list + [{"number": item.number, "opened_at": item.opened_at}]}}' loop: "{{ incidents.records }}" when: incidents - name: close incidents from list servicenow.itsm.incident: state: closed number: "{{ item.number }}" close_code: "Solved (Permanently)" close_notes: "Closed with ansible servicenow.itsm" other: active: false loop: "{{ incident_list }}" when: - incident_list is defined

Related Articles