Skip to content

Instantly share code, notes, and snippets.

@kd7lxl
Created December 19, 2018 05:27
Show Gist options
  • Save kd7lxl/f4b2526e0beec2475826d9ff5dcbd22c to your computer and use it in GitHub Desktop.
Save kd7lxl/f4b2526e0beec2475826d9ff5dcbd22c to your computer and use it in GitHub Desktop.
Ansible dynamic inventory from LibreNMS
#!/usr/bin/env python
import json
import urllib2
librenms = json.loads(
urllib2.urlopen(urllib2.Request(
'https://librenms.hamwan.org/api/v0/devices',
headers={'X-Auth-Token': ''},
)).read()
)
inventory = {
"_meta": {
"hostvars": {}
},
"all": [device['hostname'] for device in librenms['devices']],
"seattle": [device['hostname'] for device in librenms['devices']],
}
for key in ('os', 'sysName', 'type', 'version'):
for device in librenms['devices']:
group = device.get(key)
if not group:
continue
if not inventory.get(group):
inventory[group] = []
inventory[group].append(device['hostname'])
# converts the 'status' field to an 'available' list
inventory['available'] = [device['hostname'] for device in librenms['devices']
if int(device.get('status'))]
print json.dumps(inventory, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment