ServiceNow
  • 10 Jan 2025
  • Dark
    Light
  • PDF

ServiceNow

  • Dark
    Light
  • PDF

Article summary

Steps to Create a Dynamic Inventory

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

Related Reading: ServiceNow Inventory Plugin

Step 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

Step 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.

[defaults]
collections_path=/opt/automation-gateway/ansible/collections:/usr/share/ansible/collections
host_key_checking = False
timeout = 900
# This might need adjusting depending on specifics
interpreter_python = /bin/python3.9
INVENTORY_ENABLED = "servicenow.servicenow.now"

[persistent_connection]
command_timeout = 3600
connect_timeout = 900

Step 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 NetBox plugin configuration below connects to the public NetBox demo and returns all devices found there.

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

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

plugin: servicenow.servicenow.now
instance: xxx
username: xxx
password: xxx
use_extra_vars: yes
table: "cmdb_ci_ip_switch"
fields:
    - name
    - ip_address
    - manufacturer
selection_order: [name]
filter_results: "operational_status=1^ip_addressISNOTEMPTY^manufacturerSTARTSWITHJuniper^ORmanufacturerSTARTSWITHCisco"
groups:
    JS: "'js' in sn_name"
    CS: "'cs' in sn_name"
    CR: "'cr' in sn_name"
compose:
    ansible_connection: '"network_cli"'
    ansible_host: sn_ip_address
    ansible_user: '"xxx"'
    ansible_password: '"xxx"'
    ansible_port: "22"
    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'"
Notes:
  • If using the standard table, you can remove this line table: "cmdb_ci_ip_switch" .
  • For filter_results, you are creating a filter based in ServiceNow rules to show the devices that you need.
  • For compose: ansible_network_os, you are using jinja2 expressions.

Step 4. Confirm the plugin is working

Before invoking the plugin from IAG, the plugin should be tested 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>

Step 5. Restart IAG

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


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.