Created
December 19, 2018 05:27
-
-
Save kd7lxl/f4b2526e0beec2475826d9ff5dcbd22c to your computer and use it in GitHub Desktop.
Ansible dynamic inventory from LibreNMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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