summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorAndre keedy <andre.keedy@emc.com>2016-02-03 15:42:05 -0500
committerAndre keedy <andre.keedy@emc.com>2016-02-03 15:42:05 -0500
commit22382726fa69f40c74611a79b99845df1bd3076f (patch)
tree8bd27f0ef41dd2d7ffa5a5411e241519afc770eb /contrib
parentc433289a8b3d12950a3c42e5336ce62f127b5daf (diff)
downloadansible-22382726fa69f40c74611a79b99845df1bd3076f.tar.gz
Add rackHd inventory script
- Allow ansible to get hosts inventory from rackHD by node id
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/inventory/rackhd.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/contrib/inventory/rackhd.py b/contrib/inventory/rackhd.py
new file mode 100755
index 0000000000..f27fd5905e
--- /dev/null
+++ b/contrib/inventory/rackhd.py
@@ -0,0 +1,73 @@
+#!/usr/bin/python
+import json
+import requests
+import os
+import argparse
+import types
+
+MONORAIL_URL = 'http://localhost:8080'
+
+class OnRackInventory(object):
+ def __init__(self, nodeids):
+ self._inventory = {}
+ for nodeid in nodeids:
+ self._load_inventory_data(nodeid)
+ output = '{\n'
+ for nodeid,info in self._inventory.iteritems():
+ output += self._format_output(nodeid, info)
+ output += ',\n'
+ output = output[:-2]
+ output += '}\n'
+ print output
+
+ def _load_inventory_data(self, nodeid):
+ info = {}
+ info['ohai'] = MONORAIL_URL + '/api/common/nodes/{0}/catalogs/ohai'.format(nodeid )
+ info['lookup'] = MONORAIL_URL + '/api/common/lookups/?q={0}'.format(nodeid)
+
+ results = {}
+ for key,url in info.iteritems():
+ r = requests.get( url, verify=False)
+ results[key] = r.text
+
+ self._inventory[nodeid] = results
+
+ def _format_output(self, nodeid, info):
+ output = ''
+ try:
+ node_info = json.loads(info['lookup'])
+ ipaddress = ''
+ if len(node_info) > 0:
+ ipaddress = node_info[0]["ipAddress"]
+ output += ' "' + nodeid + '" : {\n'
+ output += ' "hosts": [ "' + ipaddress + '" ],\n'
+ output += ' "vars" : {\n'
+ for key,result in info.iteritems():
+ output += ' "' + key + '": ' + json.dumps(json.loads(result), sort_keys=True, indent=2) + ',\n'
+ output += ' "ansible_ssh_user": "renasar"\n'
+ output += ' }\n'
+ output += ' }\n'
+ except KeyError:
+ pass
+ return output
+
+try:
+ #check if monorail url(ie:10.1.1.45:8080) is specified in the environment
+ MONORAIL_URL = 'http://' + str(os.environ['MONORAIL'])
+except:
+ #use default values
+ pass
+
+# Use the nodeid specified in the environment to limit the data returned
+# or return data for all available nodes
+nodeids = []
+try:
+ nodeids += os.environ['nodeid'].split(',')
+except KeyError:
+ url = MONORAIL_URL + '/api/common/nodes'
+ r = requests.get( url, verify=False)
+ data = json.loads(r.text)
+ for entry in data:
+ if entry['type'] == 'compute':
+ nodeids.append(entry['id'])
+OnRackInventory(nodeids)