mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Address comments
This commit is contained in:
parent
083530d8fa
commit
e46074c791
1 changed files with 31 additions and 26 deletions
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
|
@ -12,13 +13,8 @@ class RackhdInventory(object):
|
||||||
self._inventory = {}
|
self._inventory = {}
|
||||||
for nodeid in nodeids:
|
for nodeid in nodeids:
|
||||||
self._load_inventory_data(nodeid)
|
self._load_inventory_data(nodeid)
|
||||||
output = '{\n'
|
|
||||||
for nodeid,info in self._inventory.iteritems():
|
for nodeid,info in self._inventory.iteritems():
|
||||||
output += self._format_output(nodeid, info)
|
print(json.dumps(self._format_output(nodeid, info)))
|
||||||
output += ',\n'
|
|
||||||
output = output[:-2]
|
|
||||||
output += '}\n'
|
|
||||||
print (output)
|
|
||||||
|
|
||||||
def _load_inventory_data(self, nodeid):
|
def _load_inventory_data(self, nodeid):
|
||||||
info = {}
|
info = {}
|
||||||
|
@ -29,28 +25,29 @@ class RackhdInventory(object):
|
||||||
for key,url in info.iteritems():
|
for key,url in info.iteritems():
|
||||||
r = requests.get( url, verify=False)
|
r = requests.get( url, verify=False)
|
||||||
results[key] = r.text
|
results[key] = r.text
|
||||||
|
|
||||||
self._inventory[nodeid] = results
|
self._inventory[nodeid] = results
|
||||||
|
|
||||||
def _format_output(self, nodeid, info):
|
def _format_output(self, nodeid, info):
|
||||||
output = ''
|
|
||||||
try:
|
try:
|
||||||
node_info = json.loads(info['lookup'])
|
node_info = json.loads(info['lookup'])
|
||||||
ipaddress = ''
|
ipaddress = ''
|
||||||
if len(node_info) > 0:
|
if len(node_info) > 0:
|
||||||
ipaddress = node_info[0]["ipAddress"]
|
ipaddress = node_info[0]['ipAddress']
|
||||||
output += ' "' + nodeid + '" : {\n'
|
output = {nodeid:{ 'hosts':[ipaddress],'vars':{}}}
|
||||||
output += ' "hosts": [ "' + ipaddress + '" ],\n'
|
|
||||||
output += ' "vars" : {\n'
|
|
||||||
for key,result in info.iteritems():
|
for key,result in info.iteritems():
|
||||||
output += ' "' + key + '": ' + json.dumps(json.loads(result), sort_keys=True, indent=2) + ',\n'
|
output[nodeid]['vars'][key] = json.loads(result)
|
||||||
output += ' "ansible_ssh_user": "renasar"\n'
|
output[nodeid]['vars']['ansible_ssh_user'] = 'monorail'
|
||||||
output += ' }\n'
|
|
||||||
output += ' }\n'
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--host')
|
||||||
|
parser.add_argument('--list', action='store_true')
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
#check if rackhd url(ie:10.1.1.45:8080) is specified in the environment
|
#check if rackhd url(ie:10.1.1.45:8080) is specified in the environment
|
||||||
RACKHD_URL = 'http://' + str(os.environ['RACKHD_URL'])
|
RACKHD_URL = 'http://' + str(os.environ['RACKHD_URL'])
|
||||||
|
@ -61,13 +58,21 @@ except:
|
||||||
# Use the nodeid specified in the environment to limit the data returned
|
# Use the nodeid specified in the environment to limit the data returned
|
||||||
# or return data for all available nodes
|
# or return data for all available nodes
|
||||||
nodeids = []
|
nodeids = []
|
||||||
try:
|
|
||||||
nodeids += os.environ['nodeid'].split(',')
|
if (parse_args().host):
|
||||||
except KeyError:
|
try:
|
||||||
url = RACKHD_URL + '/api/common/nodes'
|
nodeids += parse_args().host.split(',')
|
||||||
r = requests.get( url, verify=False)
|
RackhdInventory(nodeids)
|
||||||
data = json.loads(r.text)
|
except:
|
||||||
for entry in data:
|
pass
|
||||||
if entry['type'] == 'compute':
|
if (parse_args().list):
|
||||||
nodeids.append(entry['id'])
|
try:
|
||||||
RackhdInventory(nodeids)
|
url = RACKHD_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'])
|
||||||
|
RackhdInventory(nodeids)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
Loading…
Add table
Reference in a new issue