ServiceNow dynamic inventory

To create and manage device inventory via plugin, several steps are required. This guide covers the ServiceNow plugin.

1

Install the plugin

There is an ansible-galaxy command that can install the plugin into the IAG server. You will likely have to incorporate this command into your IAG build plan so that you do not have to do this as a manual step. For ServiceNow the command looks like this:

ansible-galaxy collection install servicenow.servicenow

2

Configure Ansible to use the plugin

There are several ways to accomplish this step. Below is a suggested method for enabling a plugin. Make sure the interpreter_python is appropriate for the environment.

/etc/ansible/ansible.cfg

This example enables the ServiceNow dynamic inventory plugin.

1[defaults]
2collections_path=/opt/automation-gateway/ansible/collections:/usr/share/ansible/collections
3host_key_checking = False
4timeout = 900
5# This might need adjusting depending on specifics
6interpreter_python = /bin/python3.9
7INVENTORY_ENABLED = "servicenow.servicenow.now"
8
9[persistent_connection]
10command_timeout = 3600
11connect_timeout = 900
3

Configure the host file

Use the ServiceNow documentation to determine the most appropriate way to configure the plugin to connect to and query from the source of truth system. The configuration of the ServiceNow plugin below connects to the public ServiceNow, applies the filters, and returns all devices found there.

Be sure to specify the host file as a .yml file.

/opt/automation-gateway/ansible/inventory/hosts.yml

1plugin: servicenow.servicenow.now
2instance: xxx
3username: xxx
4password: xxx
5use_extra_vars: yes
6table: "cmdb_ci_ip_switch"
7fields:
8 - name
9 - ip_address
10 - manufacturer
11selection_order: [name]
12filter_results: "operational_status=1^ip_addressISNOTEMPTY^manufacturerSTARTSWITHJuniper^ORmanufacturerSTARTSWITHCisco"
13groups:
14 JS: "'js' in sn_name"
15 CS: "'cs' in sn_name"
16 CR: "'cr' in sn_name"
17compose:
18 ansible_connection: '"network_cli"'
19 ansible_host: sn_ip_address
20 ansible_user: '"xxx"'
21 ansible_password: '"xxx"'
22 ansible_port: "22"
23 ansible_network_os: "'junos' if sn_name.startswith('js') or sn_name.startswith('jr') else 'ios' if sn_name.startswith('cs') or sn_name.startswith('cr') else 'unknown'"
  • If using the standard table, you can remove the line table: "cmdb_ci_ip_switch".
  • For filter_results, you are creating a filter based on ServiceNow rules to show the devices that you need.
  • For compose: ansible_network_os, you are using Jinja2 expressions.
4

Confirm the plugin is working

Before invoking the plugin from IAG, test the plugin directly from the command line using direct Ansible capability. This command will invoke the plugin and return the devices it finds.

ansible-inventory -vvv --list -i <path-to-plugin-host-file>

5

Restart IAG

Restarting IAG should trigger the plugin to execute. Once execution finishes, you should see Nautobot devices in the Ansible inventory.