If we have Ansible 2.9 or newer installed, we can download and install the ServiceNow Ansible Collection with just one command:

$ ansible-galaxy collection install servicenow.itsm

With the collection in place, we are just one Ansible playbook away from adding a configuration item into ServiceNow:

- name: Demonstrate CMDB management
  hosts: localhost
  gather_facts: false

    - name: Simulate VM creation
          id: vm1234567890

    - name: Create a new item
        name: Insert instance data into CMDB
        sys_class_name: cmdb_ci_vm_instance
        ip_address: "{{ instance.object.private_ip_address }}"
          vm_inst_id: "{{ instance.object.id }}"
      register: vm

Now, before we can run this playbook, we need to prepare instance data. But instead of adding instance information to the playbook, we will instead set some environment variables in order to keep sensitive material such as credentials safe:

$ export SN_HOST=https://snow-instance.servicenow.com
$ export SN_USERNAME=username
$ export SN_PASSWORD=password

All that we need to do now is to run the playbook:

$ ansible-playbook playbook.yaml

If nothing broke too horribly, we should now be able to see new CMDB item in the ServiceNow user interface.