Migrated from github.com/shafiqalibhai/ansible-sn-inventory
  • Python 94.6%
  • Dockerfile 5.4%
Find a file
2026-04-27 22:38:13 +00:00
.gitignore Added proxy settings 2017-11-14 09:29:59 +03:00
Dockerfile Add Dockerfile: Python 3 image for ServiceNow Ansible inventory script 2026-04-27 22:38:13 +00:00
LICENSE Added README.md and changed license to Apache 2.0 2016-06-27 17:09:52 -04:00
now.ini Added proxy settings 2017-11-14 09:17:54 +03:00
now.py Removed Anomalous backslash in string and replaced with raw string 2017-11-27 11:46:50 -05:00
README.md Update README with comprehensive project information 2026-04-27 20:32:58 +00:00

ansible-sn-inventory

Overview

Generates dynamic inventory for Ansible using the ServiceNow CMDB.

Detected project type: Python.

This repository was migrated from upstream source github.com/shafiqalibhai/ansible-sn-inventory and is preserved here for archival, reference, or continued local development.

At a glance

  • Default branch: master
  • Visibility: public
  • Size: 63 KB
  • Created: 2026-04-27
  • Last updated: 2026-04-27
  • Stars / Forks / Open issues: 0 / 0 / 0
  • License: Apache-2.0

Languages

Language Bytes Share
Python 10,249 bytes 100.0%

Repository structure

  • .gitignore (1,068 B)
  • LICENSE (11,342 B)
  • now.ini (1,646 B)
  • now.py (10,249 B)
  • README.md (5,765 B)

Getting started

Clone the repository:

git clone https://forgejo.deployview.com/ssa/ansible-sn-inventory.git
cd ansible-sn-inventory

Installation

python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

Usage

Invoke the main entry point:

python3 main.py    # or the appropriate module

Original README

The content below is preserved from the previous README. Headings have been demoted so they don't compete with the new top-level sections.

ServiceNow Ansible Inventory

Generates dynamic inventory for Ansible using the ServiceNow CMDB.

Requirements

Installation

  1. Install Ansible
  2. Install requests

Copy the inventory script now.py to the Ansible inventory location, or specify the script with -i /path/to/now.py. See the Ansible documentation on Dynamic Inventory for usage.

Usage

The ServiceNow inventory script queries hosts from the cmdb_ci_server table. By default, host targets are determined by inspecting the table columns ip_address, fqdn, and host_name in order of priority. As a result, IP addresses will be prefered over hostnames when both are defined in the server record.

Ansible groups always include the display value of the table column sys_class_name. Ansible does not support most non-word characters in group names, so the inventory script will convert group names to lowercase and non-word characters to underscore. Some examples group names include the following:

  • linux_server
  • esx_server
  • windows_server

Additional groups can be configured using the SN_GROUPS environment variable. See usage and syntax in the Environment section.

You can get a list of exposed groups by inspecting the JSON content of the inventory script. The following shell command leverages the jq utility to extract all group names.

$ ./now.py  | jq 'del(.\_meta) | keys[]'

The inventory script makes use of the top level element _meta, introduced in Ansible version 1.3 to increase performance with large number of hosts and all hostvars are prefixed with sn_. By default, the following hostvars are defined for each target host:

  • sn_name
  • sn_fqdn
  • sn_ip_address
  • sn_host_name
  • sn_sys_class_name

Additional hostvars can be added by using the environment variable SN_FIELDS. See usage and syntax in the Environment section.

The ServiceNow inventory script leverages cookies to improve performance and prevent multiple sessions being created from repetitive execution. Cookies are stored in ${HOME}/.sn_api_session using the LWP format.

Environment

The ServiceNow inventory script is configured through the now.ini config file or by using environment variables. instance=, username= and password= must be set in the config file or SN_INSTANCE, SN_USERNAME, and SN_PASSWORD environment variables must be set for the script to successfully query the ServiceNow instance CMDB. Environment variables will take precedence over config file if both are set.

This script will attempt to read configuration from a config file with the same base filename .ini if present, or now.ini if not. It is possible to create symlinks to the inventory script to support multiple configurations.

NOW_INI (optional)

The path to an INI file may also be specified via this environment variable, in which case the filename matching rules above will not apply.

SN_INSTANCE (required)

The ServiceNow instance URI. The URI should be the fully-qualified domain name, e.g. 'your-instance.servicenow.com'.

export SN_INSTANCE=your-instance.servicenow.com

SN_USERNAME (required)

The ServiceNow instance user name. The user acount should have enough rights to read the cmdb_ci_server table.

export SN_USERNAME=user.name

SN_PASSWORD (required)

The ServiceNow instance user password.

export SN_PASSWORD=user.password

SN_FIELDS (optional)

Comma seperated string providing additional table columns to add as host vars to each inventory host.

export SN_FIELDS='company,os,os_version'

Note: empty fields will be ignored and field values must be the column name, not column labels.

SN_GROUPS (optional)

Comma seperated string providing additional table columns to use as groups. Groups can overlap with SN_FIELDS.

export SN_GROUPS='manufacturer,company,os'

SN_SEL_ORDER (optional)

Comma seperated string providing ability to define selection preference order. This setting will default to 'host_name,fqdn,ip_address' if not defined.

export SN_SEL_ORDER='host_name,fqdn,ip_address'

SN_CACHE_DIR (optional)

Path to directory which will be used to store inventory cache (will be created if path does not exist, path is relative to script if not explicit). If not defined caching will be disabled.

export SN_CACHE_DIR='.cache/ansible'

SN_CACHE_MAX_AGE (optional)

Maximum age in seconds of the cache before it will be refreshed with data from the ServiceNow instance CMDB. If not defined will default to 0 (always stale).

export SN_CACHE_MAX_AGE=3600

SN_PROXY (optional)

Proxy server to use for requests to ServiceNow. This setting may also be defined with the SN_PROXY environment variable. If not defined no proxy will be used.

export SN_PROXY='http://myproxy.mydomain.com:8080'

Example

Once you confirm the dynamic inventory script is working as expected, you can tell Ansible to use the now.py script as an inventory file, as illustrated below:

ansible -i now.py all -m ping

You can narrow the selection using groups created from table columns in the CMDB (default or specified by SN_GROUPS) as illustrated below:

ansible -i now.py linux_server -m ping

If you want to verify the inventory without running the playbook use the --list-hosts flag.

Contributing

Contributions are welcome. The typical workflow is:

  1. Open an issue describing the change you'd like to make.
  2. Fork the repository (or create a feature branch if you have write access).
  3. Commit your changes with clear, descriptive messages.
  4. Open a pull request against the master branch.

Please follow the existing code style and include tests or reproduction steps where relevant.

License

This project is licensed under the Apache-2.0 license. See the LICENSE file for the full text.

Repository

  • Browse: https://forgejo.deployview.com/ssa/ansible-sn-inventory
  • Clone (HTTPS): https://forgejo.deployview.com/ssa/ansible-sn-inventory.git
  • Clone (SSH): ssh://git@forgejo.deployview.com:30143/ssa/ansible-sn-inventory.git
  • Upstream / origin: github.com/shafiqalibhai/ansible-sn-inventory

This README was generated automatically based on repository metadata, contents, and any prior README content. Edit any section above to add project-specific detail.