2016-11-02 22:47:42 +01:00
|
|
|
#!/usr/bin/env python
|
2016-02-04 22:58:10 +01:00
|
|
|
|
2017-07-10 09:09:29 +02:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-03 21:42:05 +01:00
|
|
|
import json
|
|
|
|
import os
|
2017-05-30 22:08:09 +02:00
|
|
|
import requests
|
2017-07-10 09:09:29 +02:00
|
|
|
import argparse
|
2016-02-03 21:42:05 +01:00
|
|
|
|
2016-02-03 23:00:58 +01:00
|
|
|
RACKHD_URL = 'http://localhost:8080'
|
2016-02-03 21:42:05 +01:00
|
|
|
|
2017-05-30 22:08:09 +02:00
|
|
|
|
2016-02-03 23:00:58 +01:00
|
|
|
class RackhdInventory(object):
|
2016-02-03 21:42:05 +01:00
|
|
|
def __init__(self, nodeids):
|
|
|
|
self._inventory = {}
|
|
|
|
for nodeid in nodeids:
|
|
|
|
self._load_inventory_data(nodeid)
|
2016-03-14 21:26:52 +01:00
|
|
|
inventory = {}
|
2017-05-30 22:08:09 +02:00
|
|
|
for (nodeid, info) in self._inventory.items():
|
|
|
|
inventory[nodeid] = (self._format_output(nodeid, info))
|
2016-03-14 21:26:52 +01:00
|
|
|
print(json.dumps(inventory))
|
2016-02-03 21:42:05 +01:00
|
|
|
|
|
|
|
def _load_inventory_data(self, nodeid):
|
|
|
|
info = {}
|
2017-05-30 22:08:09 +02:00
|
|
|
info['ohai'] = RACKHD_URL + '/api/common/nodes/{0}/catalogs/ohai'.format(nodeid)
|
2016-02-03 23:00:58 +01:00
|
|
|
info['lookup'] = RACKHD_URL + '/api/common/lookups/?q={0}'.format(nodeid)
|
2016-02-03 21:42:05 +01:00
|
|
|
|
|
|
|
results = {}
|
2017-05-30 22:08:09 +02:00
|
|
|
for (key, url) in info.items():
|
|
|
|
r = requests.get(url, verify=False)
|
2016-02-03 21:42:05 +01:00
|
|
|
results[key] = r.text
|
|
|
|
self._inventory[nodeid] = results
|
|
|
|
|
|
|
|
def _format_output(self, nodeid, info):
|
|
|
|
try:
|
|
|
|
node_info = json.loads(info['lookup'])
|
|
|
|
ipaddress = ''
|
|
|
|
if len(node_info) > 0:
|
2016-02-04 22:58:10 +01:00
|
|
|
ipaddress = node_info[0]['ipAddress']
|
2017-05-30 22:08:09 +02:00
|
|
|
output = {'hosts': [ipaddress], 'vars': {}}
|
|
|
|
for (key, result) in info.items():
|
2016-03-14 21:26:52 +01:00
|
|
|
output['vars'][key] = json.loads(result)
|
|
|
|
output['vars']['ansible_ssh_user'] = 'monorail'
|
2016-02-03 21:42:05 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return output
|
|
|
|
|
2016-02-04 22:58:10 +01:00
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--host')
|
|
|
|
parser.add_argument('--list', action='store_true')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
2017-07-10 09:09:29 +02:00
|
|
|
|
2016-02-03 21:42:05 +01:00
|
|
|
try:
|
2017-05-30 22:08:09 +02:00
|
|
|
# check if rackhd url(ie:10.1.1.45:8080) is specified in the environment
|
2016-02-03 23:00:58 +01:00
|
|
|
RACKHD_URL = 'http://' + str(os.environ['RACKHD_URL'])
|
2016-02-03 21:42:05 +01:00
|
|
|
except:
|
2017-05-30 22:08:09 +02:00
|
|
|
# use default values
|
2016-02-03 21:42:05 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the nodeid specified in the environment to limit the data returned
|
|
|
|
# or return data for all available nodes
|
|
|
|
nodeids = []
|
2016-02-04 22:58:10 +01:00
|
|
|
|
|
|
|
if (parse_args().host):
|
|
|
|
try:
|
|
|
|
nodeids += parse_args().host.split(',')
|
|
|
|
RackhdInventory(nodeids)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if (parse_args().list):
|
|
|
|
try:
|
|
|
|
url = RACKHD_URL + '/api/common/nodes'
|
2017-05-30 22:08:09 +02:00
|
|
|
r = requests.get(url, verify=False)
|
2016-02-04 22:58:10 +01:00
|
|
|
data = json.loads(r.text)
|
|
|
|
for entry in data:
|
|
|
|
if entry['type'] == 'compute':
|
|
|
|
nodeids.append(entry['id'])
|
|
|
|
RackhdInventory(nodeids)
|
|
|
|
except:
|
|
|
|
pass
|